Skip navigation.
Home

JPOX Introduction

1. Introduction

JPOX is a free and fully compliant implementation of the JDO1, JDO2, JDO2.1 and JPA1 specifications, providing transparent persistence of Java objects. It supports persistence to all of the major RDBMS on the market today, persistence to the DB4O object datastore, supporting all of the main Object-Relational Mapping (ORM) patterns demanded by today's applications, allows querying using either JDOQL, SQL, or JPQL, and comes with its own byte-code enhancer. There is an example which shows how to use JPOX to access DB4O.

2. Description of This Example

During this example, we will model an author object with two attributes, name and books. We use JPOX to store, delete, query and update the objects.

3. Steps of This Example

The following part shows the details of this example. You can complete it step by step.

3.1 Preparation

This example needs some files, you can get them according to the following steps:
1. Download DB4O database from http://www.db4o.com/community/. There are only some jar files. It is very easy to use this database, just add them to your classpath.
2. Download jpox-enhancer. Jpox-enhancer is dependent on some jar files. You can download it according to its guide URL http://www.jpox.org/docs/1_2/projects/enhancer.html.
After download all the files, we can prepare the classes. The author class is as following:

package decisci.demo;

public class Author {
    private int books;

    private String name;

    public Author(String name, int books) {
        this.name = name;
        this.books = books;
    }

    protected Author() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBooks() {
        return books;
    }

    public void setBooks(int books) {
        this.books = books;
    }
}

We should define its persistence definition using Meta-Data. Every class like this needs its own persistence definition. You can create a file to store this definition for every class. You also can use a file to store the definition of all the classes. The following is the MetaData:

<?xml version="1.0" encoding="UTF-8"?>
<!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="decisci.demo">
        <class name="Author">
            <field name="books" persistence-modifier="persistent"/>
            <field name="name" persistence-modifier="persistent"/>
        </class>
    </package>
</jdo>

Save this file as Author.jdo, put it at the same path to the Author class. Now we write a class to use JPOX to store, delete, query and update the objects. Before write the test class, we should make a JDO property file:

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

Save it as jpox.properties. The “formula1.db4o” is the name of the database file of DB4O. All the data is stored in this file.
A PersistenceManager provides methods for persisting of objects, querying for persisted objects, etc. We can get an instance of it according to the following code:

PersistenceManagerFactory pmf = JDOHelper
    .getPersistenceManagerFactory("jpox.properties");
PersistenceManager pm = pmf.getPersistenceManager();

After get this instance, now we can persist objects. This is performed as follows:

Transaction tx=pm.currentTransaction();
try {
    tx.begin();

    Author au = new Author("Eric Ben", 5);
    pm.makePersistent(au);

    tx.commit();
} finally {
    if (tx.isActive()) {
        tx.rollback();
    }
    pm.close();
}

Retrieve objects from persistent storage:

Transaction tx=pm.currentTransaction();
try {
    tx.begin();

    Query query = pm.newQuery(Author.class, "books > 1");
    query.setOrdering("books ascending");
    Collection result = (Collection) query.execute();
    Author auQuery = null;
    for (Iterator iter = result.iterator(); iter.hasNext();) {
        auQuery = (Author) iter.next();
        System.out.println("Author:" + auQuery.getName() + "-->"
                + auQuery.getBooks());
    }

    tx.commit();
} finally {
    if (tx.isActive()) {
        tx.rollback();
    }
    pm.close();
}

This can retrieve all the Author objects that have books more than 1, ordering them in ascending books order. We also can query objects by using some methods. For example:

pm.newQuery(Author.class, "name.indexOf(\"Eric\")>=0");
pm.newQuery(Author.class, "name.length() > 6");

To get more information, please refer to its help documentation.


Delete objects from persistent storage:

Transaction tx=pm.currentTransaction();
try {
    tx.begin();

    Query query = pm.newQuery(Author.class, "books == 5");
    Collection result = (Collection) query.execute();
    Author auQuery = null;
    for (Iterator iter = result.iterator(); iter.hasNext();) {
        auQuery = (Author) iter.next();
    }
    pm.deletePersistent(auQuery);

    tx.commit();
} finally {
    if (tx.isActive()) {
        tx.rollback();
    }
    pm.close();
}

In order to delete an object successfully, we should query the object and then delete it. The above example just deletes one object, if we want to delete some of them, for example, we want to delete all the Author objects that have books less than 3, and we can do it like this:

Transaction tx=pm.currentTransaction();
try {
    tx.begin();

    Query query = pm.newQuery(Author.class, "books < 3");
    Collection result = (Collection) query.execute();
    pm.deletePersistentAll(result);

    tx.commit();
} finally {
    if (tx.isActive()) {
        tx.rollback();
    }
    pm.close();
}

Update object:

Transaction tx=pm.currentTransaction();
try {
    tx.begin();

    Query query = pm.newQuery(Author.class, "name == \"Eric Ben\"");
    Collection result = (Collection) query.execute();
    Author auQuery = null;
    for (Iterator iter = result.iterator(); iter.hasNext();) {
        auQuery = (Author) iter.next();
    }
    auQuery.setBooks(8);

    tx.commit();
} finally {
    if (tx.isActive()) {
        tx.rollback();
    }
    pm.close();
}

It’s similar to the operation of deleting an object. First you should query the object, and then delete it.

3.2 Enhance Object

In order to run the example, the Author class should be enhanced. Otherwise an exception of ClassNotPersistenceCapableException happens. There are a few ways to enhance the class. We can use JPOX Eclipse plugin to do this. But here we do it manually by using a command. The relative files are stored like this:

Classes/decisci/demo/Author.class
Classes/decisci/demo/Author.jdo

lib/jdo2-api-2.1.jar
lib/jpox-core-1.2.2.jar
lib/jpox-enhancer-1.2.2.jar
lib/asm-3.1.jar

The following is the command to enhance the class:

Manually on Windows :
java -cp classes;lib/asm-3.1.jar;lib/jpox-core-1.2.2.jar;
lib/jdo2-api-2.1.jar;lib/jpox-enhancer-1.2.2.jar
 org.jpox.enhancer.JPOXEnhancer classes/decisci/demo/Author.jdo

[This command should be on single line]

3.3 Run the Example

After enhance the Author class, now we can run the example successfully. Make sure all the relative jar files are available in the Java CLASSPATH. Run it as a normal java project.

4. Conclusions

JPOX is an implementation of JDO. JDO (Java Data Objects) is a specification designed to provide a standardized way to make objects persistent in a database. According to this example, you can learn the basic use of JPOX. If you want to learn more about JPOX, you can visit http://www.jpox.org/.

NTnLrYFMqYSPa

dHQljgPeRIYyK

HQmGUIrWsCfpwZjLu

irruCDgmBwnSIUKEU

NIIcCKkGegQksVZVqa

Latest image