[ Team LiB ] |
12.1 ArchitectureJDO is based on the concept of persistence transparency. In other words, you write regular Java classes without worrying about conformance to any particular contract. An application uses persistent business components in a JDO environment supplied by a JDO implementation. Figure 12-1 illustrates this high-level architecture. Figure 12-1. The JDO architecture12.1.1 Business ObjectsIn JDO parlance, business objects are referred to as JDO instances. Because of JDO's transparency, JDO instances can be regular user-defined Java classes or collections, JavaBeans, or even Enterprise JavaBeans. When you introduce such a class into a JDO environment, it becomes a persistence-capable class. Example 12-1 shows a simple, persistence-capable Book class. Example 12-1. A persistence-capable book classpackage com.imaginary.ora; public class Book { String author; String isbn; String title; public Book( ) { super( ); } public Book(String auth, String num, String ttl) { super( ); author = auth; isbn = num; title = ttl; } public String getAuthor( ) { return author; } public String getIsbn( ) { return isbn; } public String getTitle( ) { return title; } public void setAuthor(String auth) { author = auth; } public void setIsbn(String num) { isbn = num; } public void setTitle(String ttl) { title = ttl; } } You should not strain yourself looking for anything unfamiliar. This is the exact same Book class you would write in a nonpersistent application. The only aspect that relates to JDO is that you must have the default constructor. Without it, some enhancers may fail during the enhancing process. I will cover the enhancing process later in the chapter. 12.1.2 ApplicationsYour application uses persistent business objects and is responsible for interacting with JDO to make those objects persistent. Example 12-2 creates a book in the data store using JDO. Example 12-2. Persisting new books using JDOpackage com.imaginary.ora; import java.util.Properties; import javax.jdo.*; public class Librarian { static public void main(String[ ] args) { PersistenceManagerFactory factory; Properties props = new Properties( ); PersistenceManager mgr; Transaction trans; // load JDO properties factory = JDOHelper.getPersistenceManagerFactory(props); mgr = factory.getPersistenceManager( ); trans = mgr.currentTransaction( ); try { Book book = new Book("Daniel C. Dennett", "0-262-54053-3", "The Intentional Stance"); trans.begin( ); mgr.makePersistent(book); trans.commit( ); } catch( Exception e ) { e.printStackTrace( ); } finally { if( trans.isActive( ) ) { trans.rollback( ); } mgr.close( ); } } } This sample method is the agent that makes your plain Book class a persistent JDO instance. It finds a persistence manager, creates a transaction, associates your new Book instance with the transaction, and finally commits the transaction. In the event of an exception, the code rolls back the transaction. The only hocus-pocus I left out is something you should be familiar with as a J2EE developer. Namely, I left out setting up the properties that help the application interact with the JDO implementation it is using. You have seen this use of properties in JDBC with the calls to DriverManager and you have seen this in JNDI with initial context properties. What is interesting about JDO properties is that many of them exist to tell the persistence manager what properties to pass to the JDBC driver that the JDO implementation is using behind the scenes. Naturally, these properties are highly dependent on your runtime environment. In addition to telling JDO how to find a JDBC driver, they tell JDO what implementation class to use for a PersistenceManagerFactory class. 12.1.3 ImplementationsA JDO implementation is to JDO as a JDBC driver is to JDBC. In other words, it is the set of concrete classes that implements the interfaces of the JDO specification. The JDO implementation performs the operations that make your business objects persist against a data store. As with JDBC drivers, some JDO implementations cost money while others are free. Sun provides a reference implementation on the JDO web site at http://java.sun.com/products/jdo. Which implementation you use depends on the runtime environment in which you intend to deploy. The entry point into a JDO implementation is the PersistenceManagerFactory. Example 12-2 showed the Librarian application using the PersistenceManagerFactory to get a PersistenceManager class. This task is in fact the PersistenceManagerFactory's raison d'être. You get access to a PersistenceManagerFactory by calling getPersistenceManagerFactory( ) in the JDO JDOHelper class. Just about all other interaction between an application and a JDO implementation occurs through the PersistenceManager you got from the factory. Example 12-2 used that PersistenceManager to manage a transaction in which a new Book was added to the library. 12.1.4 Data StoresA data store is where you store your data. JDO does not require the data store to be a relational database. While it most commonly is a relational database, it can be an object database, a CRM system, a filesystem, or something completely different. |
[ Team LiB ] |