开发者

Reference "to any" with JPA

开发者 https://www.devze.com 2023-01-17 06:09 出处:网络
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.

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 you开发者_StackOverflow社区r answer,

Dominik


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消