views:

28

answers:

2

Hi there,

is it possible to model a reference to "any" (entity) with JPA? Similar to a reference of type Object in Java, which can hold any object.

Thanks for your answer,

Dominik

A: 

According to DataNucleus this is not possible (search in this for java.lang.Object): http://www.datanucleus.org/products/accessplatform_1_1/jpa/types.html

For JDO we introduced our our Object-like abstract class called Subject which is annotated for persistence and extended all other persistable object from it. That works in JDO and I think it should work in JPA as well.

Alain O'Dea
+2  A: 

You should think about representation of the reference in database.

  • If reference is represented by a single foreign key, then referenced column should be guaranteed to be unique for all possible referenced entities. In JPA it's possible only for entites in inheritance hierarchy, so you'll get a reference to the root of some inheretence hierarchy of entites instead of Object.
  • Reference may be represented by a pair of values <type of referenced entity, foreign key>. This case is not supported by plain JPA, but supported in Hibernate by Hibernate's own @Any annotation.
axtavt
Is it possible to extend JPA with a custom reference resolver, which would do more or less the same as Hibernate's @Any? I don't think it would be a good idea to mix JPA and Hibernate entities, wouldn't it?
raymi
@raymi: There is not need to do it, Hibernate annotations work fine with JPA.
axtavt