JDO manages the lifecycle of an object, from creation (Transient) through to persistence in the datastore (Hollow, Persistent Clean) and all of the various states between these. The transition between these states are achieved by using methods on the Persistence Manager such as makePersistent(), makeTransient(), deletePersistent(), and by commiting the changes made by these operations, or by rolling them back. The various lifecycle states supported by JPOX are shown below.
![]() JDO provides a class JDOHelper that allows you to interrogate the object state. Unfortunately to get the overall object state you would have to call up to 6 methods on JDOHelper. JPOX extends JDOHelper to provide a convenience method JPOX 1.1.4 or later String state = org.jpox.JPOXHelper.getObjectState(obj); JPOX 1.2 or later String state = org.jpox.jdo.JPOXJDOHelper.getObjectState(obj);
The most basic thing you can do with JDO is persist an object. The following code is an example of how you can do this
Transaction tx=pm.currentTransaction();
try
{
tx.begin();
Product product=new Product("Sony Discman","A standard discman from Sony",49.99);
pm.makePersistent(product);
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
}
The Product object progresses from Transient (initial, unpersisted state), through to Persistent New, and then finally to Hollow when it reaches the data store (after the "commit"). If the persist failed, it would "rollback" and hence end up in the same state as when it started. The following diagram shows this graphically ![]()
When you have persisted objects you need to update them. The following code is an example of how you can do this
Transaction tx=pm.currentTransaction();
try
{
tx.begin();
String product_name=product.getName();
...
product.setPrice(75.0);
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
}
The Product object starts off in Hollow state and progresses to Persistent Clean when the user requires to read from it. It then migrates to Persistent Dirty when the price is updated. Finally it returns to Hollow when the user commits/rolls back the transaction. The following diagram shows this graphically ![]()
When you no longer need an object persisted, you can delete it. The following code is an example of how you can do this
Transaction tx=pm.currentTransaction();
try
{
tx.begin();
String product_name=product.getName();
...
pm.deletePersistent(product);
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
}
The Product object starts off in Hollow state and progresses to Persistent Clean when the user requires to read from it. It then migrates to Persistent Deleted when the deletePersistent() called. Finally it either progresses to Transient when commit is called, or returns to Hollow if it is rolled back. The following diagram shows this graphically ![]()
The following diagram shows the state transitions possible with JDO and JPOX. ![]() |