Hibernate继承映射

业务背景

最近在做的项目中,涉及到指标项的部分,分为经济、技术、工料三类指标。三类指标有很多共有特性,也各有一些细微的区别。
在做实体映射的时候,最初的版本是各个指标相互独立,各不相干。也就是所有属性都是每类指标各有一套,看上去就显得冗余。

关于继承的最初考虑

最开始的时候就考虑提取一个基类,把共有属性提取出来。
各个子类还是使用@Entity和@Table分别作Hibernate映射。

public class BaseIndex {

    private String id;
    private String description;
    private Boolean keyindex;
    private String remark;
    private TradeType tradeType;

    public BaseIndex() {

    }

    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "uuid")
    @Column(length = 32)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    ……
}
@Entity
@Table(name = "normquantityindexdict")
public class NormQuantityIndex extends BaseIndex {

    private String unit;
    private NormQuantityIndex parentIndex;

    public NormQuantityIndex() {
        super();
        parentIndex = null;
    }
}

结果会报如下错误:
这里写图片描述

解决方案

经查得知,不能简单地提取基类,会导致Hibernate找不到相关信息。需要使用另一个基类注解:@MappedSuperclass。

@Documented
@Target({TYPE})
@Retention(RUNTIME)
public @interface MappedSuperclass {
}

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.
Mapping information may be overridden in such subclasses by using the AttributeOverride and AssociationOverride annotations or corresponding XML elements.
即:设计一个类,它的映射信息会应用于其派生类。该类本身没有对应的数据库表。派生类可以覆盖基类中的属性,使用AttributeOverride 和AssociationOverride 注解。

以上的继承映射满足组件化的需求(即避免重复代码及映射配置,便于维护)。


继承还有另一层需求,就是多态!

解决方案2

使用@Inheritance注解。

/**
 * Defines the inheritance strategy to be used for an entity class
 * hierarchy. It is specified on the entity class that is the root of
 * the entity class hierarchy.
*/
@Target({TYPE})
@Retention(RUNTIME)
public @interface Inheritance {
    InheritanceType strategy() default SINGLE_TABLE;
}
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
}

三种继承策略如下:
- SINGLE_TABLE:公共属性公共表,独立属性公共表;
使用监视器属性区分具体子类(type),设置监视器的注解为@DiscriminatorColumn(类注解);
子类的属性映射配置时,需要设置为允许为空或者默认值;
其实就是一张表,通过一个type之类的属性来区分派生类;
Hibernate的save等操作都是针对父类,过程中对子类属性的读取和写入通过强转实现;譬如save的时候会根据派生类的类型把type字段赋值
- TABLE_PER_CLASS:公共属性独立表,独立属性独立表;
主键生成策略不能使用GenerationType.IDENTITY;
多个子类表对应的主键也不能重复;
- JOINED:公共属性公共表,独立属性独立表;
其实就是父类一张表,各个子类各一张表;然后子类通过设置外键挂到父类上;
操作的是时候是针对父类做操作,过程中对子类属性的读取和写入通过强转实现;save的时候会执行两个insert语句,向两张表分别插入一条记录;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值