标签:jpa继承映射策略 jpa继承映射 jpa映射策略 jpa jpaj映射
注:这里所说的实体指的是@Entity注解的类
继承映射使用@Inheritance来注解,它的strategy属性的取值由枚举InheritanceType来定义(包括SINGLE_TABLE、TABLE_PER_CLASS、JOINED,分别对应三种继承策略)。@Inheritance注解只能作用于继承结构的超类上。如果不指定继承策略,默认使用SINGLE_TABLE。
实例如下:
package com.mikan; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; 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.Table; @Entity @Table(name = "EMP") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "emp_type") public class Employee implements Serializable { private static final long serialVersionUID = -7674269980281525370L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected Integer empId; @Column protected String name; // getter/setter方法 } package com.mikan; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue("FT") public class FullTimeEmployee extends Employee { private static final long serialVersionUID = 9115429216382631425L; @Column private Double salary; // getter/setter方法 } package com.mikan; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue("PT") public class PartTimeEmployee extends Employee { private static final long serialVersionUID = -6122347374515830424L; @Column(name = "hourly_wage") private Float hourlyWage; // getter/setter方法 }其中,超类的@DiscriminatorColumn注解可以省略,默认的“discriminator列”名为DTYPE,默认类型为STRING。
package com.mikan; import java.io.Serializable; import javax.persistence.Column; 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.Table; @Entity @Table(name = "EMP") @Inheritance(strategy = InheritanceType.JOINED) public class Employee implements Serializable { private static final long serialVersionUID = -7674269980281525370L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected Integer empId; @Column protected String name; // getter/setter方法 } package com.mikan; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "FT_EMP") public class FullTimeEmployee extends Employee { private static final long serialVersionUID = 9115429216382631425L; @Column private Double salary; // getter/setter方法 } package com.mikan; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "PT_EMP") public class PartTimeEmployee extends Employee { private static final long serialVersionUID = -6122347374515830424L; @Column(name = "hourly_wage") private Float hourlyWage; // getter/setter方法 }这会映射成三个具体的表,分别是,Employee对应EMP表,字段包括empId、name;FullTimeEmployee对应FT_EMP表,字段包括empId、salary;PartTimeEmployee对应PT_EMP表,字段包括empId、hourly_wage。其中,表FT_EMP和PT_EMP中的empId作为表EMP的外键,同是它也是主键。默认情况下,使用超类的主键作为子类的主键和外键。当然,可以通过@PrimaryKeyJoinColumn注解来自己指定外键的名称,如FullTimeEmployee使用@PrimaryKeyJoinColumn(name = "FT_EMPID")注解,那么该子类实体的字段为FT_EMPID、name,FT_EMPID作为表FT_TIME的主键,同时它也是EMP表的外键。
package com.mikan; import java.io.Serializable; import javax.persistence.Column; 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.Table; @Entity @Table(name = "EMP") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Employee implements Serializable { private static final long serialVersionUID = -7674269980281525370L; @Id @GeneratedValue(strategy = GenerationType.TABLE) protected Integer empId; @Column protected String name; // getter/setter方法 } package com.mikan; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "FT_EMP") public class FullTimeEmployee extends Employee { private static final long serialVersionUID = 9115429216382631425L; @Column private Double salary; // getter/setter方法 } package com.mikan; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "PT_EMP") public class PartTimeEmployee extends Employee { private static final long serialVersionUID = -6122347374515830424L; @Column(name = "hourly_wage") private Float hourlyWage; // getter/setter方法 }这会映射成三个具体的表,分别是,Employee对应EMP表,字段包括empId、name;FullTimeEmployee对应FT_EMP表,字段包括empId、salary;PartTimeEmployee对应PT_EMP表,字段包括empId、hourly_wage。其中,表FT_EMP和PT_EMP中的empId和EMP表的empId没有任何关系。子类实体每保存一条数据,EMP表中不会插入记录。
因为TABLE_PER_CLASS策略每个表都是单独的,没有并且各表的主键没有任何关系,所以不能使用GenerationType.AUTO或GenerationType.IDENTITY主键生成策略,可以使用GenerationType.TABLE。
如果超类是抽象类,那么不会生成对应的表。如果超类是具体的类,那么会生成对应的表。以上实例使用JPA的hibernate实现测试通过。
标签:jpa继承映射策略 jpa继承映射 jpa映射策略 jpa jpaj映射
原文地址:http://blog.csdn.net/mhmyqn/article/details/37996673