如何处理指向通用接口的指针的 JPA 注释 [英] How to handle JPA annotations for a pointer to a generic interface

查看:28
本文介绍了如何处理指向通用接口的指针的 JPA 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个泛型类,它也是一个映射的超类,它有一个私有字段,该字段保存指向另一个相同类型对象的指针:

I have a generic class that is also a mapped super class that has a private field that holds a pointer to another object of the same type:

@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"属性.

My problem is that Eclipse is showing an error in the file at the OneToOne "Target Entity "T" for previous is not an Entity." All of the implementations of MyIfc are, in fact, Entities. I should also add that each concrete implementation that inherit from MyClass uses a different value for T (because T is itself) so I can't use the "targetEntity" attribute.

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

I guess if there is no answer then I'll have to move this JPA annotation to all the concrete subclasses of MyClass. It just seems like JPA/Hibernate should be smart enough to know it'll all work out at run-time. Makes me wonder if I should just ignore this error somehow.

推荐答案

我的问题是 Eclipse 在 OneToOne 的文件中显示错误目标实体T"不是实体.

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 通用实体类 Mappedsuperclass 是不可能的!(关于 EclipseLink 的线程非常相似):

Yes, and even if T was extending an Entity, I am not aware of any JPA provider supporting this (that's just not part of the JPA spec anyway). For more feedback have a look at JPA Generic entities classes Mappedsuperclass are not possible! (very similar thread about EclipseLink):

不,您将无法使实体通用.提供者将无法将关系映射到由通用定义定义的特定类型,因为当实体是在代码中而不是在定义实体的地方创建时分配的.请记住,在指定泛型时,集合(在这种情况下)仅限于这些类型.Provider 不可能在每个实体实例的基础上如此限制.在某些情况下,更改类型可能会导致为单个实体实例映射完全不同的表,这绝对不受支持.

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.

这篇关于如何处理指向通用接口的指针的 JPA 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆