JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer
JPOX 1.1 Runtime
Runtime Tools
Queries
RDBMS Datastores
Persistence Manager

As you read in the guide for PersistenceManagerFactory, to control the persistence of your objects you will require at least one PersistenceManagerFactory. Once you have obtained this object you then use this to obtain a PersistenceManager. A PersistenceManager provides access to the operations for persistence of your objects. This short guide will demonstrate some of the more common operations.

You obtain a PersistenceManager as follows

PersistenceManager pm = pmf.getPersistenceManager();

In general you will be performing all operations on a PersistenceManager within a transaction, whether your transactions are controlled by your J2EE container, by a framework such as Spring, or by locally defined transactions. In the examples below we will omit the transaction demarcation for clarity.



Persisting an Object

The main thing that you will want to do with the data layer of a JDO-enabled application is persist your objects into the datastore. As we mentioned earlier, a PersistenceManagerFactory represents the datastore where the objects will be persisted. So you create a normal Java object in your application, and you then persist this as follows

pm.makePersistent(obj);

This will result in the object being persisted into the datastore, though clearly it will not be persistent until you commit the transaction. The LifecycleState of the object changes from Transient to PersistentClean (after makePersistent), to Hollow (at commit).

Using an Objects identity

Once you have persisted an object, it has an "identity". This is a unique way of identifying it. You can obtain the identity by calling

Object id = pm.getObjectId(obj);

So what ? Well the identity can be used to retrieve the object again at some other part in your application. So you pass the identity into your application, and the user clicks on some button on a web page and that button corresponds to a particular object identity. You can then go back to your data layer and retrieve the object as follows

Object obj = pm.getObjectById(id);


Deleting an Object

When you need to delete an object that you had previous persisted, deleting it is simple. Firstly you need to get the object itself, and then delete it as follows

Object obj = pm.getObjectById(id);  // Retrieves the object to delete
pm.deletePersistent(obj);
Modifying a persisted Object

To modify a previously persisted object you only need to enlist the (updated) object in a transaction and its changes will be propagated to the datastore at commit of the transaction.

Detaching a persisted Object

You often have a previously persisted object and you want to use it away from the data-access layer of your application. In this case you want to detach the object (and its related objects) so that they can be passed across to the part of the application that requires it. To do this you do

Object detachedObj = pm.detachCopy(obj); // Returns a copy of the persisted object, in detached state

The detached object is like the original object except that it has no StateManager connected, and it stores its JDO identity and version. It retains a list of all fields that are modified while it is detached. This means that when you want to "attach" it to the data-access layer it knows what to update.

Attaching a persisted Object

You've detached an object (shown above), and have modified it in your application, and you now want to attach it back to the persistence layer. You do this as follows

Object attachedObj = pm.makePersistent(obj); // Returns a copy of the detached object, in attached state

In versions of JPOX up to and including 1.1.0-beta-5 there was a method pm.attachCopy(Object, boolean) that performed this. This is now replaced by pm.makePersistent(Object).