1.建立基类 Person
package com.dada.hibernate;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/*这种配置的意思就是只建立一张
所有其他的类如果继承了它,并且有了自己的属性的话,
那么其他的每一个属性都会作为单独的一列加入到这张表中
优点就在于只要生成一张表
缺点就在于一条记录中好多列都是空的*/
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
//指定建立一张表
@DiscriminatorColumn(name="discriminator",discriminatorType=DiscriminatorType.STRING)
//指定的是不同实体之间区别字段的名称是 "discriminator"
@DiscriminatorValue("person")
//指明当前的实体所使用的区别于其他实体的字段值
public class Person {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}
2.建立第一个继承类Student
package com.dada.hibernate;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("student")
public class Student extends Person {
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
3.建立第二个继承类Teacher
package com.dada.hibernate;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("teacher")
public class Teacher extends Person {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
4.建立测试类Itest
package com.dada.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class Itest {
private static SessionFactory sf;
@BeforeClass
public static void beforeClass() {
//new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testSave() {
Student s = new Student();
s.setName("s1");
s.setScore(1);
Teacher t = new Teacher();
t.setTitle("chinese");
t.setName("ztd");
Session ss = sf.getCurrentSession();
ss.beginTransaction();
ss.save(s);
ss.save(t);
ss.getTransaction().commit();
}
@Test
public void testSavePerson() {
Person p = new Person();
p.setName("person");
Session ss = sf.getCurrentSession();
ss.beginTransaction();
ss.save(p);
ss.getTransaction().commit();
}
@Test
public void testLoad() {
Session session = sf.getCurrentSession();
session.beginTransaction();
Student s = (Student) session.load(Student.class,1);
System.out.println("分数:"+s.getScore());
Teacher t = (Teacher) session.load(Teacher.class, 2);
System.out.println("头衔:"+t.getTitle());
Person p = (Person) session.load(Person.class, 3);
System.out.println("姓名:"+p.getName());
Person p1 = (Person) session.load(Person.class, 1);
System.out.println("姓名:"+p1.getName());
Person p2 = (Person) session.load(Person.class,2);
System.out.println("姓名:"+p2.getName());
session.getTransaction().commit();
}
}
5.运行后的结果:
6.如果把上面的Person类改成如下:
package com.dada.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.SINGLE_TABLE)
//使用一张表
@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;
}
}
Student修改为:
package com.dada.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;
}
}
Teacher修改为:
package com.dada.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;
}
}
测试类不变,这样运行测试类之后数据库中会生成两张表,一张person用来存放数据的,跟之前的区别在于它多了一个DTYPE字段
另一张用来存放主键:
7.如果把Person类修改如下:每个类生成一张表
package com.dada.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;
}
}
那么在数据库中就会生成4张表:
第一张person
第二张student
第三张teacher
而且在person中有的列在student和teacher表中都有,而且还会包含它们自己独有的列。
它们公用的就是生成主键的表:t_gen_table
8.如果把Person修改如下:
package com.dada.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}
那么在数据库中就会生成3张表:
第一张person
第二张student
第三张teacher
其中person表存放了Person类里面所有的属性,而student,teacher则存放它们对应类所独有的属性,它们之间是一外键的形式关联起来的,person表存放id,name属性,而student或者teacher存放score和title属性,只需要通过一个id来关联起来。