【文档摘要】J2EE Persistence – Introduction to the Java Pers

Entity Inheritance

1. Abstract Entities

对于abstract class,可以被query。query的操作是对于所有的concrete类生效。
@Entitypublic abstract class Employee {    @Id    protected Integer employeeId;    ...}
@Entitypublic class FullTimeEmployee extends Employee {    protected Integer salary;    ...}@Entitypublic class PartTimeEmployee extends Employee {    protected Float hourlyWage;}

2. Mapped Superclass

Entity可能从非Entity继承,但是在父类中含有需要持久化的字段信息。因为没有@Entity,所以,Employee不能被query或通过entityManager使用。公用的字段是在具体子类定义的。

@MappedSuperclasspublic class Employee {    @Id    protected Integer employeeId;    ...}
@Entitypublic class FullTimeEmployee extends Employee {    protected Integer salary;    ...}@Entitypublic class PartTimeEmployee extends Employee {    protected Float hourlyWage;    ...}

3. Non-Entity Superclass

entity class可能含有concrete或abstract的非entity class,非entity superclass的状态是不可持久化的,非entity superclass也不能被query或通过entityManager使用

4. Entity Inheritance Mapping Strategies

使用@Inheritance标记策略

包含如下三种:

SINGLE_TABLE,JOINED,TABLE_PER_CLASS

1) Single Table

需要添加Discriminator Column来标示是哪一个具体子类型,@DiscriminatorColumn

2) Table Per Concrete Class

这种方法对多态支持不好,经常需要用UNIION进行查询(某些JPA的provider不一定支持这种继承实现方式, 如Glassfish)

3) Joined

这种方法需要额外的join操作

参考资料:

https://docs.oracle.com/javaee/7/tutorial/doc/persistence-intro002.htm

单表继承例子:http://www.thejavageek.com/2014/05/14/jpa-single-table-inheritance-example/

哪里有意志存在,哪里就会有出路。

【文档摘要】J2EE Persistence – Introduction to the Java Pers

相关文章:

你感兴趣的文章:

标签云: