SpringDataJPA笔记(14)-Inheritance注解详解之SINGLE_TABLE
在JPA中使用映射注解Inheritance,有三种策略属性
SINGLE_TABLE – 将所有父类和子类集合在一张表
TABLE_PER_CLASS – 每个子类会生成一张单独的表,父类可以查询所有子类的表数据,参考上一篇笔记Union查询
JOINED – 每个类分别生成一张单独的表,但是每张表只有自己的属性,没有父类的属性,通过外键关联的形式使两张表关联起来
public enum InheritanceType {
/** A single table per class hierarchy. */
SINGLE_TABLE,
/** A table per concrete entity class. */
TABLE_PER_CLASS,
/**
* A strategy in which fields that are specific to a
* subclass are mapped to a separate table than the fields
* that are common to the parent class, and a join is
* performed to instantiate the subclass.
*/
JOINED
}
简单写个demo测试一下
分别新建三个实体类
@Data
@Table(name = "inheritance_single_tb")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class InheritanceSingleEntity implements Serializable {
private static final long serialVersionUID = 8139674112977338603L;
@Id
private Long id;
@Column(name = "single_name")
private String singleName;
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(name = "inheritance_single_one_tb")
public class InheritanceSingleOneEntity extends InheritanceSin