单表设计(SINGLE_TABLE)
将所有的表都放入父类的一张表中, 需要一个DiscriminatorColumn来区别不同的子类。
创建Person,Student,Teacher类Student和Teacher都继承Person。
Person类中使用@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
指定表的设计模式
@DiscriminatorColumn(name=“discriminator”,
discriminatorType=DiscriminatorType.STRING)
package com.hibernate.model;
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)
@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;
}
}
Student类
package com.hibernate.model;
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;
}
}
Teacher
package com.hibernate.model;
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;
}
}
三张独立的表(TABLE_PER_CLASS)
三张独立的表,id不能相同
person类
package com.hibernate.model;
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)
//t_gen_table用来记录id值。保证不重复
@TableGenerator(
name="t_gen",//此处的名字要和下面generator="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.hibernate.model;
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.hibernate.model;
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;
}
}
JOINED模式
创建两张表,子表保存自己的功能。父表保存子表的id和子表创建的名字。
Person类
package com.hibernate.model;
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;
}
}
student类
package com.hibernate.model;
import javax.persistence.Entity;
@Entity
public class Student extends Person {
private int score;
public int getScore() {
return score;
} //Person p = Person(load(1));
public void setScore(int score) {
this.score = score;
}
}
Teacher类
package com.hibernate.model;
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;
}
}