问题
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