【问题标题】:Hibernate Schema-validation error for the inherited entity class继承实体类的休眠模式验证错误
【发布时间】:2020-11-06 22:32:45
【问题描述】:

这是我继承的子实体如下:-

@AllArgsConstructor
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "SUB_TABLE")
@DiscriminatorValue("R")
public class SubTable extends BaseAudit {

    @Column(name = "REV", updatable = false, insertable = false)
    private long rev;
    @Column(name = "REVTYPE", updatable = false, insertable = false)
    private long revType;

    @Column(name = "active_from", updatable = false, insertable = false)
    private Instant activeFrom;
    @Column(name = "active_to", updatable = false, insertable = false)
    private Instant activeTo;

    public SubTable() {
    }
  }

我的基本实体如下:-

@Entity
@Table(name = "BASE_AUDIT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "derive_type")
public abstract class BaseAudit {

    @Id
    @GeneratedValue
    @Column(name = "id")
    public Long id;

    @Version
    @Column(name = "version")
    public Long version;

    @CreatedBy
    @Column(name = "created_by")
    public String createdBy;

    @CreatedDate
    public Instant created;

    @LastModifiedBy
    @Setter(AccessLevel.PRIVATE)
    @Column(name = "updated_by")
    public String updatedBy;

    @LastModifiedDate
    @Setter(AccessLevel.PRIVATE)
    public Instant updated;

    @Column(name = "derive_type", insertable = false, updatable = false)
    public String deriveType;

}

我将有许多子实体将继承自这个 BaseAudit 实体。我收到以下错误:-

init 方法调用失败;嵌套异常是 javax.persistence.PersistenceException:[PersistenceUnit:默认] 无法建立 Hibernate SessionFactory;嵌套异常是 org.hibernate.tool.schema.spi.SchemaManagementException: 架构验证:表 [base_audit] 中缺少列 [active_to]

因此它抱怨缺少基本实体的 active_to 列。但是此列来自子实体。我不需要这个基础实体。我将有许多其他子实体,只有常见的子实体在基础实体中。我怎样才能做到这一点?

【问题讨论】:

    标签: java spring-boot hibernate jpa


    【解决方案1】:

    使用@Inheritance(strategy = SINGLE_TABLE) 完全符合它在锡上所说的 - 所有关于父实体和子实体的数据都挤在一个表中。

    这意味着 @Table(name = "SUB_TABLE") 将被忽略,因为您特别指示将 SubTable 的所有数据放入父级的 BASE_AUDIT 表中。由于BASE_AUDIT 没有必要的列(REVREVTYPEactive_fromactive_to),您会遇到架构验证错误。

    您确定@MappedSuperclass 不是您想要的,而不是@Inheritance

    【讨论】:

      猜你喜欢
      • 2018-07-21
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多