How to declare a Interface Descriptor for independent entities under common interface using EclipseLink?
We have the situation where we need to associate different entities under one attribute and use this attribute in a query. The project uses JPA with EclipseLink as the implementation.
Example code to illustrate the point: (the actual problem is a lot more complex)
interface I1 {
public int getData();
}
@Entity
class E1 implements I1 {
public int id;
public String someData;
public int getData() { return someData; }
}
@Entity
class E2 implements I1 {
public int id;
public String someOtherData;
public int getData() { return someOtherData; }
}
@Entity
class Test {
// we want to handle this as if it was a real entity using the I1 Interface
// Since it is a oneToMany mapping we cannot use eclipselinks
/开发者_运维问答/ VariableOneToOne mapping
// something like this should be possible
// SELECT t.refToEntities From Test t where t.data == 'Some String'
public List<I1> refToEntities;
}
Do to historical reasons the entities do not share a inheritance relation but they have common data which is abstracted away under a common interface. We are now facing the problem that we a have another entity which wants to relate to these entities using the common interface in a one to many relationship. Ideally the relation should be queryable using JPQL but using EclipseLink specific methods is fine as well.
To summarize we want to have a VariableOneToOne Mapping for a OneToMany relation.
The documentation I have found so far indicate that it is possible to build a RelationalDescriptor for interfaces but it is unclear on how to get this descriptor into the session in a JPA Scenario and how to setup a InterfacePolicy in order to use the interface as the common type for the underlying entities.
SO does anybody know how to do this or can point us to working code?
You would probably be best off using inheritance instead of the interface.
Or have a list of each entity type instead of just the interface.
EclipseLink only supports a VariableOneToOne not a VariableOneToMany, please log an enhancement request for this.
You can define a RelationalDescriptor for an interface using a SessionCustomizer, just instantiate it, set the interface using descriptor.setJavaInterface(Il.class) and add any common query keys.
You could potentially use this interface as the target of a OneToManyMapping (defined in code). You would not be able to join across it in JPQL though.
精彩评论