Get Object By Id

There are different ways to get a JDO instance:

For example you may acquire access to JDO instances using the JDO Query interface as described in chapter "Queries".

Furthermore, if you have or can construct a valid ObjectId, you can get an instance via the PersistenceManagers getObjectById method:

PersistenceManager.getObjectById(Object oid, boolean validate);

This method locates a persistent instance in the cache of instances managed by this PersistenceManager. If an instance with the same ObjectId is found it is returned. Otherwise, a new instance is created and associated with the ObjectId.

If the instance does not exist in the data store, then this method will not fail. However, a request to access fields of the instance will throw an exception.

The oid parameter object might have been returned by an earlier call to PersistenceManager.getObjectId, or might have been constructed by the application.

The validate flag indicates whether the existence of the instance is to be validated or not. If this flag is set to true, it will be verified, if the instance exists in the datastore and a JDOObjectNotFoundException is thrown, if it does not exist.

There is also a method getObjectById (Object oid) which behaves exactly as the method getObjectById (Object oid, boolean validate) with the validate flag set to true.