esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

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.

  1. Install LIBeLIS (or your preferred JDO implementation)

  2. Set the classpath

    This code sample is only viewable to esus.com members
    Login or become a member!


  3. 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:

    This code sample is only viewable to esus.com members
    Login or become a member!


    Address.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


    Role.java:

    This code sample is only viewable to esus.com members
    Login or become a member!


  4. Compile these business objects.

  5. 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:

    This code sample is only viewable to esus.com members
    Login or become a member!


    Now run your enhancer tool on the three business objects that we wish to be made capable of being persistent.

    This code sample is only viewable to esus.com members
    Login or become a member!


    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:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


    Yeah, right! Imagine writing this stuff yourself! Notice the value of an Enhancer tool!

  6. Create the database.

    Use the MySQL administration tool to create a database, eg. jdotest.

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  7. 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:

    This code sample is only viewable to esus.com members
    Login or become a member!


    Look at what has been created in the database jdotest:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


  8. 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:

    This code sample is only viewable to esus.com members
    Login or become a member!


    Check the database and notice the table updates:

     
    This code sample is only viewable to esus.com members
    Login or become a member!


    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!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.