|
Java™ by example!
|
|
|
How do I get started with JDO?
JDO allows transparent persistency of Java objects. It is just an API with a bunch of vendors implementing it, the so called JDO implementations (for a list, look at the links in the section products of the JDO category). This following simple example shows you the required steps necessary to store a business object Member in a database using JDO. We'll use the JDO implementation of LIBeLIS. You can download LiDO Community Edition from (www.libelis.com). Because it's free, MySQL was chosen as a data store (www.mysql.com) and the JDBC driver for MySQL can be downloaded from mmmysql.sourceforge.net.
- Install LIBeLIS (or your preferred JDO implementation)
- Set the classpath
- Write your business objects.
Let's write an object Member. To make it a bit more interesting, let's have this object be composed of other ones, like Address and Role. Later on, all of these objects need to be made persistent. Member.java:
Address.java:
Role.java:
- Compile these business objects.
- Enhance the .class files.
All persistent classes must implement the PersistenceCapable interface. You can do this yourself but this is a rather difficult and errorprone process, or you could use a tool that all JDO vendors supply. Such a tool takes the .class file and modifies its bytecodes so that it implements the PersistenceCapable interface (bytecode modification is typically done using the Apache's BCEL library). What you need to supply to the enhancer tool is a persistence descriptor or metadata, an XML file that indicates what classes and fields need to be made persistent. The metadata file for our example is the following: metadata.jdo:
Now run your enhancer tool on the three business objects that we wish to be made capable of being persistent.
After this step, Member.class, Address.class, and Role.class have been modified. Out of curiosity, let's decompile Role.class and find out what has been changed. Decompiled Role.class:
Yeah, right! Imagine writing this stuff yourself! Notice the value of an Enhancer tool!
- Create the database.
Use the MySQL administration tool to create a database, eg. jdotest.
- Generate the database schema.
A JDO implementation will come with a tool that will create the necessary database tables (or generate a script that creates the tables). With LIBeLIS LiDO, schema generation goes like this:
Look at what has been created in the database jdotest:
- Create a test application.
The following program will create three members and persist them. Before we can persist anything, we get a PersistenceManager from a PersitenceManagerFactory. The factory itself is created using a number of properties that describe what data source is being used. The PersistenceManager is a facade object that serves as the main entry point for JDO operations. All persitent operations in JDO must happen in a transaction. To do that, ask the PersistenceManager for a transaction with the call pm.currentTransaction() and start it with t.begin(). Once t.commit() has been executed, the objects changed by the transaction are made persistent. Main.java:
Check the database and notice the table updates:
Look at the other Q/A's in this category for more JDO examples.
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|