JPOX
JPOX
 JPOX Version 1.0
Configuration | Tutorials | Worked Examples | Developer
JPOX Tutorials
JPOX and EclipseJDO
Background - EclipseJDO

EclipseJDO is a plugin for Eclipse IDE that provides you a powerful interface for developing applications using JDO. EclipseJDO helps you to manage your JDO metadata files and provides you an easy and transparent way of enhancing files using the Sun Reference JDO Enhancer. This tutorial will teach you how to create a java persistent class and its metadata with EclipseJDO. Within one hour you will be persisting your Java Objects with JPOX using EclipseJDO.

This tutorial requires that you have installed the EclipseJDO plugin in Eclipse and you have downloaded JPOX and its required libraries. You can download the EclipseJDO plugin at the EclipseJDO web site

EclipseJDO requires:

Create your JDO Project

1. Select the menu item File > New > Project...

2. Select JDO > JDO Java Project then click on next

3. Fill Project Name field as below and click on next.

4. Configure the libraries needed by JPOX as below.

5. You can also add your database driver libraries and then, click on finish.

6. Makes sure your JDO project properties is using the Sun Reference Enhancer JDO Compliant 1.01, and click on OK.

Create your persistent objects

Now create and define what objects you will make persistent.

1. Select the menu item File > New > Class...

2. Fill the package name (model) and Name (Person) for the class, and click on finish to create the java class.

3. Add some attributes to your java class as below

4. Select the menu item File > New > Other...

5. Select JDO > Metadata file then click on next

6. Select the java class Person then click on finish

7. Here you are free to configure the JDO metadata with optional JPOX features, for example you can specify the column-name.

8. Now save and close the metadata editor

Create a main class to launch the example

To run the example, you need to create a main class to launch the application and make your object persistent.

1. Select the menu item File > New > Class

2. Fill the package name (main) and Name (Main) for the class, and click on Finish.

3. Add a main method and follow the below code to complete the sample.

package main;
import java.util.Properties;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;
import model.Person;
public class Main
{
    public static main(String[] args)
    {
        // Properties for connection
        Properties properties = new Properties();
        properties.setProperty("javax.jdo.PersistenceManagerFactoryClass","org.jpox.PersistenceManagerFactoryImpl");
        properties.setProperty("javax.jdo.option.ConnectionDriverName","oracle.jdbc.driver.OracleDriver");
        properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:oracle:thin:@127.0.0.1:1521:MYORACLE");
        properties.setProperty("javax.jdo.option.ConnectionUserName","SYSTEM");
        properties.setProperty("javax.jdo.option.ConnectionPassword","MANAGER");
        properties.setProperty("org.jpox.autoCreateSchema","true");
        properties.setProperty("org.jpox.validateTables","false");
        properties.setProperty("org.jpox.validateConstraints","false");
        
        // PMFactory creation and getting the PM instance
        PersistenceManagerFactory pmfactory = JDOHelper.getPersistenceManagerFactory(properties);
        PersistenceManager pm = pmfactory.getPersistenceManager();
        
        try
        {
            pm.currentTransaction().begin(); // opening a transaction

            Person dude = new Person(); // creates one Person
            dude.setName("Dude name");
            dude.setAge(20);
            pm.makePersistent(dude); // makes it persistent

            pm.currentTransaction().commit(); // committing the transaction
        }
        finally
        {
            // if the transaction is still active, rolls it back
            if( pm.currentTransaction().isActive() )
            {
                pm.currentTransaction().rollback();
            }
            pm.close(); // close the PM
        }
    }
}
                

4. Make sure you have the correct properties for the database connection.

5. Now run the example. Select the menu item Run > Run As > Java Application...

6. You can open your database to see that the object has been persisted.

Problems, or want to know more ?

This is the end of the JPOX EclipseJDO tutorial. We hope that it was an easy start for using JPOX with the help of Eclipse and EclipseJDO for development.

Problems with JPOX? Please post at our forum.

Problems with EclipseJDO? Please post at the EclipseJDO web site

The JPOX team