With
datastore identity
you are leaving the assignment of id's to DataNucleus and your class will
not
have a field for this identity - it will be added to the datastore representation by DataNucleus.
It is, to all extents and purposes a
surrogate key
that will have its own column in the
datastore. To specify that a class is to use
datastore identity
with JDO, you add the
following to the MetaData for the class.
<class name="MyClass" identity-type="datastore">
...
</class>
or using JDO2 annotations
@PersistenceCapable(identityType=IdentityType.DATASTORE)
public class MyClass
{
...
}
So you are specifying the
identity-type
as
datastore
. You don't need to add this
because
datastore
is the default, so in the absence of any value, it will be assumed to be
'datastore'.
|
When you have an inheritance hierarchy, you should specify the identity type in the
base
class for the inheritance tree. This is then used for all persistent classes
in the tree.
|
By choosing
datastore identity
you are handing the process of identity generation to the
JDO implementation. This does not mean that you haven't got any control over how it does this.
JDO 2 defines many ways of generating these identities and DataNucleus supports all of these and
provides some more of its own besides.
Defining which one to use is a simple matter of adding a MetaData element to your classes
definition, like this
<class name="MyClass" identity-type="datastore">
<datastore-identity strategy="sequence" sequence="MY_SEQUENCE"/>
...
</class>
<class name="MyClass" identity-type="datastore">
<datastore-identity strategy="identity"/>
...
</class>
or using annotations, for example
@PersistenceCapable
@DatastoreIdentity(strategy="sequence", sequence="MY_SEQUENCE")
public class MyClass
{
...
}
Some of the datastore identity strategies require additional attributes, but the specification is
straightforward.
See also :-
When using
datastore identity
, the class has no associated field so you can't just
access a field of the class to see its identity - if you need a field to be able to access the
identity then you should be usingapplication identity.
There are, however, ways to get the identity for the datastore identity case, if you have the
object.
Object id = pm.getObjectId(obj);
Object id = JDOHelper.getObjectId(obj);
You should be aware however that the "identity" is in a complicated form, and is not available as
a simple integer value for example. Again, if you want an identity of that form then you should use
application identity
When implementing
datastore identity
all JDO implementations have to provide a public
class that represents this identity. If you call
pm.getObjectId(...)
for a class using
datastore identity you will be passed an object which, in the case of DataNucleus will be of type
org.datanucleus.identity.OIDImpl
. If you were to call "toString()" on this object you would
get something like
1[OID]mydomain.MyClass
This is made up of :-
1 = identity number of this object
class-name
The definition of this datastore identity is JDO implementation dependent. As a result you
should not use the
org.datanucleus.identity.OID
class in your application if you want to
remain implementation independent
DataNucleus allows you the luxury of being able to
provide your own datastore identity class
so you can have whatever formatting you want for identities.
If you have the JDO identity then you can access the object with that identity like this
Object obj = pm.getObjectById(id);
You can also access the object from the object class name and the toString() form of the
datastore identity (e.g "1[OID]mydomain.MyClass") like this
Object obj = pm.getObjectById(MyClass.class, mykey);