【问题标题】:Why is the MappedSuperClass annotation no longer valid in combination with @Inheritance when migrating to Hibernate 5.4.x?为什么在迁移到 Hibernate 5.4.x 时 MappedSuperClass 注释与 @Inheritance 结合不再有效?
【发布时间】:2019-03-14 10:36:33
【问题描述】:

我正在迁移几个代码库以使用 Hibernate 5.4.x 而不是 Hibernate 5.2.x。

对于我使用的抽象基类

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractPersistentJPAObject extends AbstractPersistentObject {
    // some properties
}

但是,ORM 会通过错误消息对此进行抱怨

不能同时使用@Inheritance@MappedSuperclass 注释实体

这不是 Hibernate

问题

  1. 这是错误还是功能?如果是一项功能:此更改背后的基本原理是什么?
  2. 可以做些什么来规避这种情况?
  3. 如果“规避”不是一个有效的想法:应如何更改上述代码片段以将其正确迁移到 Hibernate >= 5.4.x。

欢迎任何可靠的答案。

【问题讨论】:

    标签: java hibernate jpa jpa-2.1 hibernate-5.x


    【解决方案1】:

    我的答案如下:

    1. 映射不正确根据JPA 2.2 specification@MappedSuperclass@Inheritance不能一起使用。看起来,上面的映射在早期的 Hibernate 版本中是容忍的。然而,这种支持似乎在 Hibernate 5.4.x 中被删除了。

    2. 要解决上述特定场景中的问题,您可以将@MappedSuperclass 替换为@Entity,它应该可以正常工作。

    Hibernate forum也有类似的问题。

    您还可以查看HHH-13217,其中 Gail Badner 和 Vlad Mihalcea(均为 Hibernate 开发人员)正在讨论此问题。

    在下一个即将发布的 5.4.2 版本中,如果 @Inheritence@MappedSuperclass 一起使用,它将被忽略(请参阅 Github 上的相关 PR)。

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2012-06-18
      • 2012-03-28
      • 2011-09-21
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      相关资源
      最近更新 更多