Person,Student,Teacher各创建一个表,主键用一个中间表生成。

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.TableGenerator;

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@TableGenerator(
        name="t_gen",
        table="t_gen_table",
        pkColumnName="t_pk",
        valueColumnName="t_value",
        pkColumnValue="person_pk",
        initialValue=1,
        allocationSize=1
        )
public class Person {
    private int id;
    private String name;
   
    @Id
    @GeneratedValue(generator="t_gen", strategy=GenerationType.TABLE)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;

@Entity
public class Student extends Person {
   
    private int score;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
   
}

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;

@Entity
public class Teacher extends Person {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

   
}

 

测试类:

package com.bjsxt.hibernate;

import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateORMappingTest {
    private static SessionFactory sessionFactory;
   
    @BeforeClass
    public static void beforeClass() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    @AfterClass
    public static void afterClass() {
        sessionFactory.close();
    }
   
    @Test
    public void testSave() {
        Student s = new Student();
        s.setName("s1");
        s.setScore(80);
        Teacher t = new Teacher();
        t.setName("t1");
        t.setTitle("中级");
       
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(s);
        session.save(t);
        session.getTransaction().commit();
        session.close();
    }
    @Test
    public void testLoad() {
        testSave();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Student s = (Student)session.load(Student.class, 1);
        System.out.println(s.getScore());
        Person p = (Person)session.load(Person.class, 2);
        System.out.println(p.getName());
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
    }
   
   
    public static void main(String[] args) {
        beforeClass();
    }
}

hibernate 继承映射关系( TABLE_PER_CLASS)的更多相关文章

  1. hibernate 继承映射关系( SINGLE_TABLE)

    三种继承映射关系.   1,SINGLE_TABLE   person student  teacher 在一个表中,student和teacher继承自person,通过一个Discriminato ...

  2. hibernate 继承映射关系( JOINED)

    一个主表,其他的表每个都有自己的表来装填自己特有的部分,共同的部分就放在主表中.   package com.bjsxt.hibernate; import javax.persistence.Ent ...

  3. EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子

    本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ...

  4. Hibernate关联映射关系

    Hibernate关联映射关系 一.双向一对多关联映射关系:当类与类之间建立了关联,就可以方便的从一个对象导航到另一个或另一组与它关联的对象(一对多双向关联和多对一双向关联是完全一样的) 1.1创建实 ...

  5. entity framework里的继承映射关系TPH、TPT和TPC

    本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ...

  6. EF——继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子 05 (转)

    EF里的继承映射关系TPH.TPT和TPC的讲解以及一些具体的例子   本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF ...

  7. hibernate笔记--继承映射关系的三种实现方式

    单表继承映射(一张表): 假设我们现在有三个类,关系如下: Person类有两个子类Student和Teacher,并且子类都具有自己独有的属性.这种实体关系在hibernate中可以使用单表的继承映 ...

  8. 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法

    几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...

  9. Hibernate继承映射(@Inheritance)

    继承映射在 Annotation 中使用 @Inheritance 注解,并且需要使用 strategy 属性指定继承策略,继承策略有 SINGLE_TABLE.TABLE_PER_CLASS 和 J ...

随机推荐

  1. MySQL的事务理解

    在学习事务这一概念前,我们需要需要构思一个场景 场景构思 假设该场景发生于一个银行转账背景下,月中,又到了发工资的日子.学校打算给A老师发放一个月的工资.(此处,我们假设转账都是由人工操作的),整个过 ...

  2. ubantu的python2与python3的相关兼容更新问题

    Ubuntu14.04, 系统内同时装了Python3.3 和 2.7用sudo apt-get install python-pipsudo apt-get install python3-pip分 ...

  3. IntelliJ IDEA 常用设置/快捷键

    经常用到 IntelliJ IDEA 编写java,由于不时需要重装系统,所以Mark一下一些基本的设置选项,以便查询,这篇帖子会一直更新,只要有常用的新的设置或者快捷键 一.常用设置 显示代码行号 ...

  4. 2018 ACM-ICPC 南京网络赛

    Problem A Problem B Problem C Problem D Problem E Problem F Problem G Problem H Problem I Problem J ...

  5. win7 office2016 激活(2018.6.17测试可用)

    坑比的一天,啥也没学,净用来折腾了office2016的安装了. 虽然有个wps可以用,但是真心的卡啊,用不惯就卸载了.虽然是卸载了,也埋了很多坑给我. 还是说office2016吧,网上到处找激活工 ...

  6. Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. ubuntu 16.04.1 LTS 初始化

    gcc环境------------------sudo apt-get update && \sudo apt-get install build-essential software ...

  8. 杭电oj 1000

    今天开始和一个认识的学弟刷题. 学弟是个大牛,我还是个菜鸟.嘿嘿. 杭电第一题我就wrong了好几次. #include <iostream> using namespace std; i ...

  9. Largest Divisible Subset -- LeetCode

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  10. python3开发进阶-Django框架起飞前的准备

    阅读目录 安装 创建项目 运行 文件配置的说明 三个组件 一.安装(安装最新LTS版) Django官网下载页面 根据官方的图版本,我们下载1.11版本的,最好用! 有两种下载方式一种直接cmd里: ...