SpringDataJPA笔记(14)-Inheritance注解详解之SINGLE_TABLE

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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值