6.1 Finding an Object by Identity


In Chapter 1, we introduced the concept of persistence by reachability and how an entire object graph of dependent objects is persisted when an object is saved. The simplest way to find an object is through its identifier. In Chapters 3 and 5, we covered the concepts surrounding object identity and how the getObjectID() can be used. You may recall that the JDOHelper class exposes a getObjectID(Object pc) method that returns the JDO identity associated with the parameter.

The PersistenceManager interface exposes a corresponding getObjectById() method that can be used in the application code to retrieve the instance through its JDO identity.

This example code taken from the example ObjectIdentityExample.java in Chapter 5 demonstrates how the identity of a persistent object can be used to retrieve it:

 
 PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); tx.begin(); Author author =   new Author(     "Sameer Tyagi",     new Address(       "1 Main Street", "Boston", "MA", "01822")); // make the Author and all its fields persistent pm.makePersistent(author); tx.commit(); // obtain the object id  Object oid = pm.getObjectId(author);  pm.close(); // conceptually the oid is now available and can be reused PersistenceManager pm2 = pmf.getPersistenceManager(); Transaction tx2 = pm2.currentTransaction(); tx2.begin(); Author author2 = (Author)  pm.getObjectById(oid, true);  // Should print out the name "Sameer Tyagi" below System.out.println("Author is: " + author2.getName()); tx2.commit(); pm2.close(); 

Object ID must be serializable

The object specifying the identity and returned by getObjectId() is required to be serializable so that applications can store it.


Figure 6-1 shows the logical flow that the JDO runtime goes through when the getObjectById() method is invoked. The JDO implementation tries to locate the object in the PersistenceManager 's cache. If it is found, the object is verified with the underlying datastore and then returned. If the object cannot be found in the cache with the corresponding identity, the JDO implementation creates an instance, assigns it the specified identity, and returns it. Note that the JDO implementation verifies state with the underlying datastore only if the object instance in cache is non-transactional. Transactional instances do not necessarily need their state to be verified because the state is synchronized with the datastore.

Figure 6-1. Flowchart for getObjectById() .

graphics/06fig01.jpg

The semantics associated with retrieving an object instance in JDO by using the JDO identity and the getObjectById method are in many ways analogous to the getHomeHandle() method in the EJB specifications. EJB can serialize and store the home handle and use it to re-obtain a reference to the remote home object at a later time.

Object ID State

getObjectById returns a hollow instance, which can be deleted afterward by some other client of the datastore. When the application tries to use the object, i.e., when it is resolved, a JDOUserException is thrown.




Core Java Data Objects
Core Java Data Objects
ISBN: 0131407317
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net