JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
JPOX Guides
JDO Guides
JPA Guides
JPOX - Tutorial for JDO using DB4O
Background

We saw in the JDO Tutorial the overall process for adding persistence to a simple application. In that tutorial we persisted the objects to an RDBMS. Here we show the differences when persisting instead to the db4o object datastore.

  1. Step 1 : Design your domain/model classes as you would do normally
  2. Step 2 : Define their persistence definition using Meta-Data.
  3. Step 3 : Compile your classes, and instrument them (using the JPOX enhancer).
  4. Step 4 : Generate the database tables where your classes are to be persisted.
  5. Step 5 : Write your code to persist your objects within the DAO layer.
  6. Step 6 : Run your application.

The tutorial guides you through this. You can obtain the code referenced in this tutorial from SourceForge (one of the files entitled "jpox-samples-tutorial-*").



Step 1 : Create your domain/model classes

The model classes are exactly the same here, so please refer to the JDO Tutorial for details.



Step 2 : Define the Persistence for your classes

When defining the persistence of the classes, the only difference would be that we could omit the <column> tags from the MetaData since they are for relational datastores only and db4o would make no use of them. It would, however, be perfectly safe to leave them in and JPOX will ignore them. For completeness, here is the MetaData without any ORM components.

<?xml version="1.0"?>
<!DOCTYPE jdo PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN" 
    "http://java.sun.com/dtd/jdo_2_0.dtd">
<jdo>
    <package name="org.jpox.samples.tutorial.jdo">
        <class name="Product" identity-type="datastore">
            <field name="name"/>
            <field name="description"/>
            <field name="price"/>
        </class>

        <class name="Book" identity-type="datastore">
            <field name="isbn"/>
            <field name="author"/>
            <field name="publisher"/>
        </class>
    </package>
</jdo>


Step 3 : Instrument/Enhance your classes

In the JDO Tutorial we saw that we need to bytecode enhance our classes to be persisted. This step is identical when using db4o so please refer to the original tutorial for details.



Step 4 : Generate any schema required for your domain classes

With db4o we have no such concept as a "schema" and so this step is omitted.



Step 5 : Write the code to persist objects of your classes

We saw in the JDO Tutorial how to persist our objects using JDO API calls. This is the same when using db4o so please refer to the original tutorial for details.



Step 6 : Run your application

We saw in the JDO Tutorial how to run our JDO-enabled application. This is very similar when persisting to db4o. The only differences are

  • Put your db4o.jar in the CLASSPATH instead of the JDBC driver
  • Put the JPOX DB4O JAR in the CLASSPATH instead of JPOX RDBMS JAR
  • Edit jpox.properties to include the details for your DB4O datastore

A sample jpox.properties could be like this

javax.jdo.PersistenceManagerFactoryClass=org.jpox.jdo.JDOPersistenceManagerFactory
javax.jdo.option.ConnectionURL=db4o:file:db4o.db


Any questions?

As you can see changing between an RDBMS and db4o is trivial. You don't need to change your actual classes, or persistence code at all. It's simply a change to your runtime details.

If you have any questions about this tutorial and how to develop applications for use with JPOX please read the online documentation since answers are to be found there. If you don't find what you're looking for go to our Forums.

The JPOX Team