迁移到Hibernate 5.4.1时,为什么MappedSuperClass注释与@Inheritance不再有效?

问题描述 投票:2回答:1

我正在迁移几个代码库以使用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 <= 5.2.x的问题,现在我想知道为什么不应再允许这样做了。

问题

  1. 这是一个错误还是一个功能?如果一个功能:这种变化背后的理由是什么?
  2. 可以采取哪些措施来规避这种情况?
  3. 如果“规避”不是一个有效的想法:如何更改上述代码片段以正确地将其迁移到Hibernate> = 5.4.x.

任何坚实的答案欢迎。

java hibernate jpa jpa-2.1 hibernate-5.x
1个回答
2
投票

我的答案如下:

  1. 根据JPA 2.2 specification@MappedSuperclass@Inheritance不能一起使用,映射是不正确的。看来,上面的映射在早期的Hibernate版本中是可以容忍的。但是,这种支持似乎在Hibernate 5.4.x中被删除了。
  2. 要解决上述特定情况下的问题,您可以将@MappedSuperclass替换为@Entity,它应该可以正常工作。

Hibernate forum也有类似的问题。

您还可以查看HHH-13217,其中Gail Badner和Vlad Mihalcea(两个Hibernate Developers)正在讨论此问题。

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

© www.soinside.com 2019 - 2024. All rights reserved.