DataNucleus, Eclipse Dali, JPA

The Eclipse Dali project provides a powerful development environment for Java Persistence. DataNucleus does not stay behind, and permits the powerful DataNucleus persistence engine to be combined with Eclipse Dali for development.

In this (5 mins) tutorial, we use Eclipse Dali to reverse engineer a database table (ACCOUNT) and generate a persistent class (Account). The DataNucleus Eclipse plug-in is used to enhance the persistent class before running the application.

Requirements

For using the IDE, you must install Eclipse 3.2, Eclipse Dali and the DataNucleus Eclipse plug-in. For using the DataNucleus runtime, see JPA annotations.

Demo


Source Code

The source code for org.jpox.demo.Account class.

package org.jpox.demo;

import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Account implements Serializable {
    @Id
    @Column(name="ACCOUNT_ID")
    private BigDecimal accountId;

    private String username;

    private BigDecimal enabled;

    private static final long serialVersionUID = 1L;

    public Account() {
        super();
    }

    public BigDecimal getAccountId() {
        return this.accountId;
    }

    public void setAccountId(BigDecimal accountId) {
        this.accountId = accountId;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public BigDecimal getEnabled() {
        return this.enabled;
    }

    public void setEnabled(BigDecimal enabled) {
        this.enabled = enabled;
    }
}

The source code for org.jpox.demo.Main class.

package org.jpox.demo;

import java.math.BigDecimal;
import java.util.Random;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

public class Main
{
    public static void main(String[] args)
    {
        java.io.InputStream is = Main.class.getClassLoader().getResourceAsStream("PMFProperties.properties");
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(is);
        PersistenceManager pm = pmf.getPersistenceManager();
        try
        {
            pm.currentTransaction().begin();
            Account acc = new Account();
            BigDecimal dec = new BigDecimal(new Random().nextInt());
            acc.setAccountId(dec);
            acc.setEnabled(BigDecimal.ONE);
            pm.makePersistent(acc);
            pm.currentTransaction().commit();
            System.out.println("Account "+dec+" was persisted.");
        }
        finally
        {
            if( pm.currentTransaction().isActive() )
            {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    }
}

The source code for PMFProperties.properties file.

javax.jdo.PersistenceManagerFactoryClass=org.jpox.PersistenceManagerFactoryImpl
javax.jdo.option.ConnectionDriverName=oracle.jdbc.driver.OracleDriver
javax.jdo.option.ConnectionURL=jdbc:oracle:thin:@127.0.0.1:1521:XE
javax.jdo.option.ConnectionUserName=test
javax.jdo.option.ConnectionPassword=password

org.jpox.autoCreateSchema=true
org.jpox.metadata.validate=false
org.jpox.autoStartMechanism=XML
org.jpox.autoCreateTables=true
org.jpox.validateTables=false
org.jpox.autoCreateColumns=true
org.jpox.autoCreateConstraints=true
org.jpox.validateConstraints=false
org.jpox.autoCreateSchema=true
org.jpox.rdbms.stringDefaultLength=255

The database schema model.

CREATE TABLE Account (
        ACCOUNT_ID NUMBER NOT NULL,
        username VARCHAR2(255),
        enabled NUMBER(1 , 0) NOT NULL
    );

ALTER TABLE Account ADD CONSTRAINT Account_PK PRIMARY KEY (ACCOUNT_ID);