3.6.4.1. Datastore Identity
The JPA specification requires you to declare one or more identity fields in your persistent classes. OpenJPA fully supports this form of object identity, called application identity. OpenJPA, however, also supports datastore identity. In datastore identity, you do not declare any primary key fields. OpenJPA manages the identity of your persistent objects for you through a surrogate key in the database.
You can control how your JPA datastore identity value is generated through OpenJPA’s
org.apache.openjpa.persistence.DataStoreId class annotation.
This annotation has strategy and
generator properties that mirror the same-named properties on the standard jakarta.persistence.GeneratedValue annotation described in Id of the JPA Overview.
To retrieve the identity value of a datastore identity entity, use the OpenJPAEntityManager.getObjectId(Object entity) method.
See OpenJPAEntityManager for more information on the OpenJPAEntityManager.
import org.apache.openjpa.persistence.*;
@Entity
@DataStoreId
public class LineItem {
... no @Id fields declared ...
}
Internally, OpenJPA uses the public
org.apache.openjpa.util.Id class for datastore identity objects.
When writing OpenJPA plugins, you can manipulate datastore identity objects by casting them to this class.
You can also create your own Id instances and pass them to any internal OpenJPA method that expects an identity object.
In JPA, you will never see Id instances directly.
Instead, calling OpenJPAEntityManager.getObjectId on a datastore identity object will return the Long surrogate primary key value for that object.
You can then use this value in calls to EntityManager.find for subsequent lookups of the same record.