8.8. JDO Identity Management

Each PersistenceManager is responsible for managing the JDO identities of the persistent objects in its cache. The following methods allow you to interact with the management of JDO identities.

public Class getObjectIdClass (Class pcClass);

Returns the JDO identity class used for the given persistence-capable class. If the given class does not use application identity, returns null.

public Object newObjectIdInstance (Class pcClass, Object pkOrString);

This method is used to re-create JDO identity objects from the string returned by their toString method, or, in the case of single field identity, from the primary key value. Given a persistence-capable class and a JDO identity string or primary key object, the method constructs a JDO identity object. Using the getObjectById method described below, this identity object can then be employed to obtain the corresponding persistent instance.

[Note]Note

Kodo also allows you to pass the primary key value of datastore identity instance into this method to create a datastore identity object.

public Object getObjectId (Object pc);

Returns the JDO identity object for a persistent instance managed by this PersistenceManager. This method is similar to JDOHelper.getObjectId.

public Object getObjectById (Object oid, boolean validate);
public Object getObjectById (Object oid);

These methods returns the persistent instance corresponding to the given JDO identity object. If the instance is already cached, the cached version is returned. Otherwise, a new instance is constructed, and may or may not be loaded with data from the datastore.

If the validate parameter of the method is true, the JDO implementation will throw a JDOObjectNotFoundException if the datastore record for the given JDO identity does not exist. Otherwise, the implementation may return a cached object without validating it. The implementation might return a hollow instance even when no cached object exists, and an exception will not be thrown until you attempt to access one of the object's persistent fields.

Invoking the method version without a validate parameter is equivalent to using a validate parameter of true.

public Object[] getObjectsById (Object[] oids, boolean validate);
public Object[] getObjectsById (Object[] oids);
public Collection getObjectsById (Collection oids, boolean validate);
public Collection getObjectsById (Collection oids);

These method are exactly equivalent to the singular getObjectById methods, but acts on an array or collection of identity objects. They may be more efficient than calling the singular method for each identity object.

public Object getObjectById (Class pcClass, Object pkOrString);

This is a convenience method equivalent to using newObjectIdInstance to construct an identity object with the given Class and Object, then calling getObjectById with that identity object and a validate parameter of true. The code below demonstrates this equivalence.

PersistenceManager pm = ...;
Object pkValue = ...;

Object oid = pm.newObjectIdInstance (Magazine.class, pkValue);
Magazine mag1 = (Magazine) pm.getObjectById (oid, true);

Magazine mag2 = (Magazine) pm.getObjectById (Magazine.class, pkValue);
assertTrue (mag1 == mag2);

 

Skip navigation bar   Back to Top