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

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

问题描述

我有一个通用类,也具有保存的指针相同类型的另一个目的私有字段映射的超类:

  @MappedSuperclass
公共抽象类MyClass的<吨延伸MyIfc< T>>
    实现MyIfc< T>
    {        @OneToOne()
        @JoinColumn(NAME =previous,可为空=真)
        私人ŧprevious;             ...
             }

我的问题是,Eclipse的是显示在一对一目标实体T在文件中的错误为previous不是一个实体。所有MyIfc的实现的是,事实上,实体。我还要补充一点,每一个从MyClass的继承具体实现使用T的一个不同的值(因为T是本身),所以我不能用targetEntity属性。

我想,如果没有答案,然后我就必须把这个JPA注释移动到MyClass的所有具体子类。它只是看起来的JPA一样/休眠应该足够聪明,知道在运行时,它会所有工作了。让我不知道是否我应该只是忽略这个错误不知。


解决方案

  

我的问题是,Eclipse的是显示在一对一目标实体T在文件中的错误为previous不是一个实体。


是的,即使 T 正在伸出一个实体 I 的是不知道任何JPA提供支持这一点(这只是不是JPA规范的一部分反正)。对于更多的反馈看一下 JPA通用实体类Mappedsuperclass是不可能的!(有关的EclipseLink非常相似的线程):


  

没有您将无法使实体通用。提供者将无法映射到由通用定义作为这种定义的特定类型的关系中的code不其中实体被定义创建实体时被分配。指定泛型集合(在这种情况下)仅限于这些类型时记住。该供应商不可能是这种限制性在每个实体实例的基础。在某些情况下,改变的类型,可能会导致完全不同的表被映射为一个单一的实体实例和肯定是不支持的。


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;

             ...
             }

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.

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.

解决方案

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

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):

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天全站免登陆