Can DataNucleus persist an abstract base class parameterized with Generics?

安稳与你 提交于 2019-12-11 06:52:42

问题


Using DataNucleus, I have been happy using an abstract base class to provide a 'long' ID to the sub-classes (please note the primitive type).

When adapting an example from JPA I got the idea to parameterize the base class. The purpose was to support different ID types, such as String.

@PersistenceCapable
@Inheritance(strategy=InheritanceStrategy.SUBCLASS_TABLE)
@Version(strategy=VersionStrategy.VERSION_NUMBER, column="jdo_version")
public abstract class VersionedIdEntity<P>
    implements Serializable {

  static final long serialVersionUID = 1L;

  @PrimaryKey
  @Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
  private P id;

  public P getId() {
    return id;
  }
}

Generics made me use the wrapped Java type 'Long', so sub-classes were declared like this:

public class Account extends VersionedIdEntity<Long>

My switch to the new base class caused an error stating that no implementation for 'java.lang.Object' was found.

org.datanucleus.exceptions.NucleusUserException: Field "VersionedIdEntity.id" is declared as a reference type (interface/Object) but no implementation classes of "java.lang.Object" have been found!

Is the error related to type erasure?

Additional questions would be (1) how JPA deals with it and (2) whether I can restrict the type P to types persistable in DataNucleus, such as:

<? extends PersistableType>

While I could not find help elsewhere, the question could have been asked before. In that event, a brief pointer would be much appreciated.


回答1:


You mean does JDO define that an implementation should support this? Answer : no. JDO defines the valid PK types, and what is a "P" ? it is Object as far as a class is concerned (i.e (java.lang.)Object, since it doesn't have the real type there). Neither does JPA define it for that matter.



来源:https://stackoverflow.com/questions/11658992/can-datanucleus-persist-an-abstract-base-class-parameterized-with-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!