orm - 如何处理指向通用接口(interface)的指针的 JPA 注释

标签 orm jpa interface generics annotations

我有一个泛型类,它也是一个映射父类(super class),它有一个私有(private)字段,其中包含指向另一个相同类型对象的指针:

@MappedSuperclass
public abstract class MyClass<T extends MyIfc<T>>
    implements MyIfc<T>
    {

        @OneToOne()
        @JoinColumn(name = "previous", nullable = true)
        private T previous;

             ...
             }

我的问题是 Eclipse 在 OneToOne 的文件中显示错误“目标实体“T”之前不是实体。” MyIfc 的所有实现实际上都是实体。我还应该补充一点,从 MyClass 继承的每个具体实现都使用不同的 T 值(因为 T 本身就是),所以我不能使用“targetEntity”属性。

我想如果没有答案,那么我将不得不将此 JPA 注释移动到 MyClass 的所有具体子类。看起来 JPA/Hibernate 应该足够聪明,知道它会在运行时完成。让我想知道我是否应该以某种方式忽略这个错误。

最佳答案

My problem is that Eclipse is showing an error in the file at the OneToOne "Target Entity "T" for previous is not an Entity."



是的,即使 T正在扩展 Entity ,我不知道有任何 JPA 提供者支持这一点(无论如何,这不是 JPA 规范的一部分)。如需更多反馈,请查看 JPA Generic entities classes Mappedsuperclass are not possible! (关于 EclipseLink 非常相似的线程):

No you will be unable to make the Entities generic. The provider will be unable to map the relationship to the specific type defined by the generic definition as this type is assigned when the Entity is created in code not where the Entity is defined. Remember when designating Generics the Collection (in this case) is limited only to those types. The Provider can not possibly be this restrictive on a per Entity instance basis. In some cases changing the type may result in entirely different tables being mapped for a single Entity instance and that is definitely not supported.

关于orm - 如何处理指向通用接口(interface)的指针的 JPA 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808251/

相关文章:

maven - 对 EclipseLink 与 org.eclipse.persistence.jpa 的依赖关系

java - 如何防止使用 Hibernate 的 Java 应用程序丢失更新?

jpa - EclipseLink : No Persistence provider for EntityManager

java - Java 发明了接口(interface)吗?

orm - 同时使用单例应用程序类时使用Sugar ORM

c# - 使用微型 ORM 时的最佳策略?

javascript - 在 Sequelize js 的范围内同时使用 "AND"和 "OR"

error-handling - Sequelize 模型重复值检查

delphi - 扩展TWebBrowser外部对象来执行Delphi代码: how to access my form components?

oop - 为什么类的私有(private)部分被视为接口(interface)?