JPA关系和多态性 [英] JPA relations and polymorphism
问题描述
我具有以下类结构(为清楚起见,省略了详细信息):
I have the following class structure (details are omitted for clarity):
例如:
@Entity
class A{
@OneToMany
private List<B> b;
@OneToOne
private C c;
}
interface B {
}
@Entity
@Inheritance
abstract class Babstract implements B {
}
@Entity
@PrimaryKeyJoinColumn
class B1impl extends Babstract {
}
@Entity
@PrimaryKeyJoinColumn
class B2Impl extends Babstract {
}
我想加载所有属于C的B.A类将B与C相关联.B和C彼此不知道彼此的存在,因此我希望保持这种状态.我不知道B的实际实现类型,也不太在意,因为我使用的是抽象数据类型B(接口).实现会随时间而变化,例如,将来可以在新版本中添加B的新实现.
I want to load all B's belonging to C. Class A relates B to C. B and C are unaware of each others existence and I like to keep it that way. I don't know the actual implementing type of B and I don't really care because I use the abstract data type B (the interface). The implementation can vary over time, for example, a new implementation of B can be added in the future in a new release.
JPA是否有可能,我该怎么做?如果我是正确的话,我就不能使用targetEntity属性,因为我不知道实现类,也不在乎. B是多态的.
Is this possible with JPA and how do I do that? I can't use the targetEntity attribute, if I'm correct, because I don't know the implementing class and I don't care. B is polymorph.
推荐答案
JPA没有接口支持,但是某些JPA提供程序却有.如果使用EclipseLink,则可以使用@VariableOneToOne映射来映射到接口.
JPA does not have interface support, but some JPA providers do. If using EclipseLink you can use a @VariableOneToOne mapping to map to an interface.
现在,如果B接口只有一个当前的实现者,而这仅仅是将来某个可能的时间",那么我只需将targetEntity设置为Babstract.如果确实确实发生了将来某个可能的时间"(可能不会发生),则可以更改映射.
Now, if the B interface only has one current implementer, and this is just a "some possible time in the future", then I would just set the targetEntity to Babstract. If "some possible time in the future" actually does occur (probably will not), then you can change the mappings then.
看, http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces
这篇关于JPA关系和多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!