|
As you can see in Listing 3, my triathlon JDO now has a key managed by the Google infrastructure, and I've added a few standard methods (toString, hashCode, and equals) that help tremendously for debugging, logging, and of course, proper functionality. Rather than write them myself, I'm using the Apache commons-lang library (see Resources). I also added a constructor that'll make creating fully initialized objects a lot easier as compared to calling a lot of setter methods.
I've kept my JDO simple on purpose, but as you can see, there isn't much to it (that is, I've left out any relationships and omitted getters and setters for brevity's sake). You simply model your domain and then decorate the model with a few annotations, and Google takes care of the rest.
With an object defined as persistence-capable, there's one last step. To interact with the underlying data-store, you need to work with a PersistenceManager, which is a JDO standard class that does what its name implies: saves, updates, retrieves, and removes objects from an underlying data store (much like Hibernate's Session object). This class is created via a factory (PersistenceManagerFactory), which is rather heavyweight; thus, Google recommends creating a singleton object that manages a single instance of the factory (which will then return a proper PersistenceManager when you need it). Accordingly, I can define a simple singleton object for returning an instance of a PersistenceManager, as shown in Listing 4:
Listing 4. A simple singleton for returning a PersistenceManager instance
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
public class PersistenceMgr {
private static final PersistenceManagerFactory instance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PersistenceMgr() {}
public static PersistenceManager manufacture() {
return instance.getPersistenceManager();
}
}
As you can see, my PersistenceMgr is quite simple. The one method, manufacture, returns a PersistenceManager instance from a single instance of a PersistenceManagerFactory. You'll also notice that there is no Google-specific code in Listing 4 or any other listing leveraging JDO — all references are to standard JDO classes and interfaces.
The two newly defined Java objects reside in my project's src directory, and I added the commons-lang library to the war/WEB-INF/lib directory.
With a simple triathlon JDO POJO defined and a PersistenceMgr object handy, I'm good to go. All I need now is a way to capture triathlon information. |
|