Any JDO-enabled application will require at least one PersistenceManager (PM). This is obtained from the PersistenceManagerFactory for the datastore.

The simplest way of creating a PersistenceManager Image 1: image is as follows

  1. PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);
  2. PersistenceManager pm = pmf.getPersistenceManager();

A PersistenceManager is the key to all persistence operations in JDO. With it you can persist, update, delete, and retrieve objects from the datastore. A PersistenceManager has a single transaction.

Persist Objects

To persist an object, the object must first be marked as persistable using MetaData (XML/Annotations). Then you would start the PM transaction, and use makePersistent as follows

  1. PersistenceManager pm = pmf.getPersistenceManager();
  2. Transaction tx = pm.currentTransaction();
  3. try
  4. {
  5. // Start the transaction
  6. tx.begin();
  7. // Create the object to persist
  8. MyClass obj = new MyClass();
  9. // Persist it to the datastore
  10. pm.makePersistent(obj);
  11. // Commit the transaction, flushing the object to the datastore
  12. tx.commit();
  13. }
  14. catch (Exception e)
  15. {
  16. ... handle exceptions
  17. }
  18. finally
  19. {
  20. if (tx.isActive())
  21. {
  22. // Error occurred so rollback the transaction
  23. tx.rollback();
  24. }
  25. pm.close();
  26. }

The makePersistent method of PersistenceManager makes the object persistent in the datastore, and updates the ‘state’ of the object from Transient (at the start) to Hollow (after commit() of the transaction).

When an object is persisted, if it has any other objects referenced from that object they also will be made persistent. This is referred to as persistence-by-reachability. The main benefit of this is that if you have an object graph to persist, then you don’t need to call makePersistent() on all objects, instead just using one that can be used to find all of the others. persistence-by-reachability is also run at the time of calling commit() on the transaction. This has the effect that if you had called makePersistent() on an object and that had persisted another object, and before commit you had removed the relation to this other object, then at commit() the reachability algorithm will find that this other object is no longer reachable and will remove it from persistence.

Retrieve Objects

So we’ve made some of our objects persistent, and now we want to retrieve them in our application. Here’s one way of retrieving objects of a particular type.

  1. tx = pm.currentTransaction();
  2. try
  3. {
  4. tx.begin();
  5. Extent e = pm.getExtent(mydomain.MyClass.class, true);
  6. Iterator iter=e.iterator();
  7. while (iter.hasNext())
  8. {
  9. MyClass my_obj=(MyClass)iter.next();
  10. ...
  11. }
  12. tx.commit();
  13. }
  14. catch (Exception e)
  15. {
  16. if (tx.isActive())
  17. {
  18. tx.rollback();
  19. }
  20. }

The Extent interface is one of the ways to retrieve your objects. The others use the Query interface, allowing more precise filtering over the objects returned.

Update Objects

To update an object we firstly retrieve it, as above, and then we call any of its mutator methods. For example

  1. tx = pm.currentTransaction();
  2. try
  3. {
  4. tx.begin();
  5. Extent e = pm.getExtent(mydomain.MyClass.class, true);
  6. Iterator iter=e.iterator();
  7. while (iter.hasNext())
  8. {
  9. MyClass my_obj=(MyClass)iter.next();
  10. my_obj.setValue(25.0); // Change the value
  11. ...
  12. }
  13. tx.commit();
  14. }
  15. catch (Exception e)
  16. {
  17. if (tx.isActive())
  18. {
  19. tx.rollback();
  20. }
  21. }

When setValue() is called on the persistent object this change is intercepted by JDO and the value change will be automatically sent to the datastore …​ transparently!

Delete Objects

So we can persist objects, and retrieve them. Now we want to remove one from persistence.

  1. try
  2. {
  3. tx = pm.currentTransaction();
  4. tx.begin();
  5. ... (code to retrieve object in question) ...
  6. pm.deletePersistent(my_obj);
  7. tx.commit();
  8. }
  9. catch (Exception e)
  10. {
  11. if (tx.isActive())
  12. {
  13. tx.rollback();
  14. }
  15. }

Making an object transient

As we have seen in the JDO States guide, an object can have many possible states. When we want to take an object and work on it, but removing its identity we can make it transient. This means that it will retain the values of its fields, yet will no longer be associated with the object in the datastore. We do this as follows

  1. try
  2. {
  3. tx = pm.currentTransaction();
  4. tx.begin();
  5. ... (code to retrieve object in question) ...
  6. pm.makeTransient(my_obj);
  7. tx.commit();
  8. }
  9. catch (Exception e)
  10. {
  11. if (tx.isActive())
  12. {
  13. tx.rollback();
  14. }
  15. }
  16. ... (code to work on "my_obj")