JDO : Application Identity

With application identity you are taking control of the specification of id's to DataNucleus. Application identity requires a primary key class (unless you have a single primary-key field in which case the PK class is provided for you) , and each persistent capable class may define a different class for its primary key, and different persistent capable classes can use the same primary key class, as appropriate. With application identity the field(s) of the primary key will be present as field(s) of the class itself. To specify that a class is to use application identity , you add the following to the MetaData for the class.

<class name="MyClass" objectid-class="MyIdClass">
    <field name="myPrimaryKeyField" primary-key="true"/>
    ...
</class>

For JDO we specify the primary-key and objectid-class . The objectid-class is optional, and is the class defining the identity for this class (again, if you have a single primary-key field then you can omit it). Alternatively, if we are using annotations

@PersistenceCapable(objectIdClass=MyIdClass.class)
public class MyClass
{
    @Persistent(primaryKey="true")
    private long myPrimaryKeyField;
}
When you have an inheritance hierarchy, you should specify the identity type in the base instantiable class for the inheritance tree. This is then used for all persistent classes in the tree. This means that you can have superclass(es) using application-identity without any identity fields/properties but using subclass-table inheritance, and then the base instantiable class is the first persistable class which has the identity field(s). This is a change from DataNucleus 2.2 where you had to have identity fields in the base persistable class of the inheritance tree.

See also :-



Primary Key

Using application identity requires the use of a Primary Key class. When you have a single primary-key field a built-in class is available meaning you don't need to define this class. This is referred to as SingleFieldIdentity . Where the class has multiple fields that form the primary key a Primary Key class must be provided.

See also :-



Compound Identity

Where one of the fields that is primary-key of your class is a persistable object you have something known as compound identity since the identity of this class contains the identity of a related class. Please refer to the docs for Compound Identity



Generating identities

By choosing application identity you are controlling the process of identity generation for this class. This does not mean that you have a lot of work to do for this. JDO defines many ways of generating these identities and DataNucleus supports all of these and provides some more of its own besides.

See also :-



Accessing the Identity

When using application identity , the class has associated field(s) that equate to the identity. As a result you can simply access the values for these field(s). Alternatively you could use a JDO identity-independent way

Object id = pm.getObjectId(obj);
Object id = JDOHelper.getObjectId(obj);


Changing Identities

JDO allows implementations to support the changing of the identity of a persisted object. This is an optional feature and DataNucleus doesn't currently support it.



Accessing objects by Identity

If you have the JDO identity then you can access the object with that identity like this

Object obj = pm.getObjectById(id);

If you are using SingleField identity then you can access it from the object class name and the key value like this

Object obj = pm.getObjectById(MyClass.class, mykey);

If you are using your own PK class then the mykey value is the toString() form of the identity of your PK class.