以Person,Student,Teacher三者的实现为例,关系如下图(Student和Teacher是Person类的子类):
三种策略:
第一种(SINGLE_TABLE):将所有类的字段都放在一个表之中,一张总表。
Person:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.DiscriminatorColumn; 4 import javax.persistence.DiscriminatorType; 5 import javax.persistence.DiscriminatorValue; 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.Inheritance; 10 import javax.persistence.InheritanceType; 11 12 @Entity 13 @Inheritance(strategy=InheritanceType.SINGLE_TABLE) 14 @DiscriminatorColumn(name="discriminator",discriminatorType=DiscriminatorType.STRING) 15 @DiscriminatorValue("person") 16 public class Person { 17 18 private Integer id; 19 20 private String name; 21 22 @Id 23 @GeneratedValue 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 }
Student:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.DiscriminatorValue; 4 import javax.persistence.Entity; 5 6 @Entity 7 @DiscriminatorValue("student") 8 public class Student extends Person { 9 10 private Integer score; 11 12 public Integer getScore() { 13 return score; 14 } 15 16 public void setScore(Integer score) { 17 this.score = score; 18 } 19 20 }
Teacher:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.DiscriminatorValue; 4 import javax.persistence.Entity; 5 6 @Entity 7 @DiscriminatorValue("teacher") 8 public class Teacher extends Person { 9 private String title; 10 11 public String getTitle() { 12 return title; 13 } 14 15 public void setTitle(String title) { 16 this.title = title; 17 } 18 }
jar包链接: https://pan.baidu.com/s/1gfh37wf 密码: rj2k
代码链接: https://pan.baidu.com/s/1c2062ek 密码: ix9t
第二种(TABLE_PER_CLASS):每个表都是完整的信息,主键都取自一个中间表,以保持三个表主键的一致性,每个类分别一张表。
Person:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.GenerationType; 6 import javax.persistence.Id; 7 import javax.persistence.Inheritance; 8 import javax.persistence.InheritanceType; 9 import javax.persistence.TableGenerator; 10 11 @Entity 12 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) 13 @TableGenerator( 14 name="t_gen", 15 table="t_gen_table", 16 pkColumnName="t_pk",//key的字段名 17 valueColumnName="t_value",//value的字段名 18 pkColumnValue="person_pk",//字段值 19 initialValue=1, 20 allocationSize=1 21 ) 22 public class Person { 23 24 private Integer id; 25 26 private String name; 27 28 @Id 29 @GeneratedValue(generator="t_gen",strategy=GenerationType.TABLE) 30 public Integer getId() { 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getName() { 39 return name; 40 } 41 42 public void setName(String name) { 43 this.name = name; 44 } 45 }
Student:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 5 @Entity 6 public class Student extends Person { 7 8 private Integer score; 9 10 public Integer getScore() { 11 return score; 12 } 13 14 public void setScore(Integer score) { 15 this.score = score; 16 } 17 18 }
Teacher:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 5 @Entity 6 public class Teacher extends Person { 7 private String title; 8 9 public String getTitle() { 10 return title; 11 } 12 13 public void setTitle(String title) { 14 this.title = title; 15 } 16 }
jar包链接: https://pan.baidu.com/s/1gfh37wf 密码: rj2k
代码链接: https://pan.baidu.com/s/1bprLc9x 密码: 3dgi
第三种(JOINED):子类对应的表和父类对应的表建立主键关联,每个子类一张表,父类对应的表保存公共属性字段,子类对应的表保存子类特有的对应字段。
Person:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Inheritance; 7 import javax.persistence.InheritanceType; 8 9 @Entity 10 @Inheritance(strategy=InheritanceType.JOINED) 11 public class Person { 12 13 private Integer id; 14 15 private String name; 16 17 @Id 18 @GeneratedValue() 19 public Integer getId() { 20 return id; 21 } 22 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 }
Student:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 5 @Entity 6 public class Student extends Person { 7 8 private Integer score; 9 10 public Integer getScore() { 11 return score; 12 } 13 14 public void setScore(Integer score) { 15 this.score = score; 16 } 17 18 }
Teacher:
1 package com.bjsxt.hibernate; 2 3 import javax.persistence.Entity; 4 5 @Entity 6 public class Teacher extends Person { 7 private String title; 8 9 public String getTitle() { 10 return title; 11 } 12 13 public void setTitle(String title) { 14 this.title = title; 15 } 16 }
jar包链接: https://pan.baidu.com/s/1gfh37wf 密码: rj2k
代码链接: https://pan.baidu.com/s/1c12sjIw 密码: 1dzr