Step Two: Persist Three "People" to the Database

Here we have some real meat. The persistPeople() method creates three people, using
the constructor that we saw in the Person.java file. Then we see JDO at work. The first
thing we do is get a persistence manager via getPersistenceManager(). Then we
create a transaction where we will do our work (and we commit that work after we do it). To persist this
object graph we simply call the makePersistentAll( Object[] ) method. The
for() loop at the bottom of the code gets the unique ID for the persistent objects, and saves
them away for later use.

Here are some of the other methods that you can call on the persistence manager. The three categories are:

Make instances persistent. Take a transient object and persist it.

Delete persistent instances. Delete the information from the datastore.

Make instances transient. Disassociate the instances from the persistence manager. The datastore doesn't have its information deleted.

Make instances persistent

Delete persistent instances

Make instances transient

makePersistent(Object o)

deletePersistent(Object o)

makeTransient(Object o)

makePersistentAll(Object[] os)

deletePersistentAll(Object[] os)

makeTransientAll(Object[] os)

makePersistentAll(Collection os)

deletePersistentAll(Collection os)

makeTransientAll(Collection os)

Step Three: Display the "People" From the Database

Our display code starts by getting the persistence manager (as all the code will do). We use the object IDs that we saved in the persistPeople() method above to give us our object back. Once we have our object, we can call the methods that the object implements -- in this case gets to give us our data back. At this point you are probably seeing there isn't a lot of code needed to persist your objects.

Step Four: Change the Name of One of the People

The code to change a Person that exists in the datastore is simple, too. It should look very
similar to the code to display the "people." Here we are creating a transaction (since we are modifying the row), changing the name using the setName() method that
we defined, and finally, committing the transaction to save the changes back. The only real
difference between this operation and working with transient objects is that we are thinking
about transactions.

Step Five: Delete a Person

Could you have guessed the code needed to delete the second person from the datastore? You know all of the information. Looking at the code below you will see that we are using the deletePersistent() method mentioned in the persistence manager methods in Step Two.

JDOEnhancer: Create a JDO Descriptor for the JDOEnhancer

We now have all the code for our application. The next step that we need is to create a JDO descriptor that the JDOEnhancer will use. "What is the JDOEnhancer?", I hear you scream. The JDO architecture is built with the idea that a JDO implementation can take the bytecode
for your classes and manipulate them to add needed functionality. For example, the
JDOEnhancer will make the class implement the PersistanceCapable interface (so you
don't have to), and may implement some of the methods in that interface. So we will see that
after we compile our code, we will have to run the JDOEnhancer to do the bytecode
manipulation (this is something that Thought Inc. doesn't like about JDO).
We need to create a descriptor file that gives information about the classes that we wish to
persist. The file looks like this:

This is a basic file, but works for our needs. There is more complicated mapping available, and you can check the spec at Section 18: XML Metadata.
Here is a slightly more complex mapping from the OpenFusion examples:

Step Three: Set Up the Database (Using Output From the JDOEnhancer)

The JDOEnhancer can create database scripts to set up the database for us, as long as we give it the
–db and –od switches. It will create lots of separate scripts, but one will be called
load_all.sql. Open that file and load it into your favorite SQL prompt (e.g., sqlplus
for Oracle).

Take a peek at the Oracle version that is created for our application:

Step Four: Run the Application

Conclusion

We have shown how to work with the new JDO standard, using the OpenFusion JDO
implementation. This is a new world, where developers can focus on their business needs and work with objects without having to be SQL gurus. I hope JDO takes off, even though some people in the industry think that the spec isn't quite there yet.