JPA : 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 using SingleFieldIdentity, where one 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.

<entity class="org.mydomain.MyClass">
    <id-class class="org.mydomain.MyIdClass"/>
        <id name="myPrimaryKeyField"/>

For JPA we specify the id field and id-class . Alternatively, if we are using annotations

public class MyClass
    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 MappedSuperclass without any identity fields/properties as superclass, 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. With JPA when you have a single-field you don't need to provide a primary key class. Where the class has multiple fields that form the primary key a Primary Key class must be provided (via the id-class ).

See also :-

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. JPA1 defines many ways of generating these identities and DataNucleus supports all of these and provides some more of its own besides.

See also :-

Changing Identities

JPA doesn't define what happens if you change the identity (an identity field) of an object once persistent. DataNucleus doesn't currently support changes to identities.

Accessing objects by Identity

You access an object from its object class name and identity "value" as follows

Object obj = em.find(MyClass.class, mykey);

If you have defined your own "IdClass" then the mykey is the toString() form if the identity of your PK class.