JPA Tools Guide (v5.2)

The DataNucleus project provides a few tools to assist in your use of DataNucleus and the JPA API.

Maven Plugin

Apache Maven is a project management and build tool that is quite common in organisations. Using DataNucleus and JPA with Maven is simple since the DataNucleus jars, JPA API jar and Maven plugin are present in the Maven central repository, so you don’t need to define any repository to find the artifacts.

pom.xml Integration

The first thing to do is identify which artifacts are required for your project, and updating your pom.xml accordingly.

Firstly, you will need the following for compile time building against the JPA API.

<project>
    ...
    <dependencies>
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.2.0-release</version>
        </dependency>
    </dependencies>
    ...
</project>

If using any DataNucleus API extensions in your code then you will also need datanucleus-core at compile time.

At runtime you will need the DataNucleus artifacts present also, so this becomes

<project>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.2.0-release</version>
        </dependency>
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-core</artifactId>
            <version>[5.1.0-release, )</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-api-jpa</artifactId>
            <version>[5.1.0-release, )</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-rdbms</artifactId>
            <version>[5.1.0-release, )</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    ...
</project>

Obviously replace the datanucleus-rdbms jar with the jar for whichever datastore you are using. If you are running the Maven "exec" plugin you may not need the "runtime" specifications.

Please note that you can alternatively use the convenience artifact for JPA+RDBMS (when using RDBMS).

<project>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-accessplatform-jpa-rdbms</artifactId>
            <version>5.1.0-release</version>
            <type>pom</type>
        </dependency>
    </dependencies>
    ...
</project>

Enhancement and SchemaTool

Now that you have the DataNucleus jars available to you, via the repositories, you want to perform DataNucleus operations. The primary operations are enhancement and SchemaTool. If you want to use the DataNucleus Maven plugin for enhancement or SchemaTool add the following to your pom.xml

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.datanucleus</groupId>
                <artifactId>datanucleus-maven-plugin</artifactId>
                <version>5.0.2</version>
                <configuration>
                    <api>JPA</api>
                    <persistenceUnitName>MyUnit</persistenceUnitName>
                    <log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

This will use your Maven dependencies defined for your project, so will need to have datanucleus-core.jar, and either of datanucleus-api-jdo.jar or datanucleus-api-jpa.jar depending on what type of metadata is being used. SchemaTool will also need the datanucleus-{datastore}.jar for whichever datastore is being used (where you will create the schema).

The executions part of that will make enhancement be performed immediately after compile, so automatic. See also the Enhancer docs

To run the enhancer manually you do

mvn datanucleus:enhance

DataNucleus SchemaTool is achieved similarly, via

mvn datanucleus:schema-create

Eclipse Plugin

Eclipse provides a powerful development environment for Java systems. DataNucleus provides its own plugin for use within Eclipse, giving access to many features of DataNucleus JPA from the convenience of your development environment.

Plugin Installation

The DataNucleus plugin requires Eclipse 3.1 or above. To obtain and install the DataNucleus Eclipse plugin select

Help -> Software Updates -> Find and Install

On the panel that pops up select Search for new features to install. The select New Remote Site and in that new window set the URL as http://www.datanucleus.org/downloads/eclipse-update/ and the name as DataNucleus.

Now select the site it has added "DataNucleus", and click "Finish". This will then find the releases of the DataNucleus plugin. Select the latest version of the DataNucleus Eclipse plugin. Eclipse then downloads and installs the plugin. Easy!

Plugin configuration

The DataNucleus Eclipse plugin allows saving of preferences so that you get nice defaults for all subsequent usage. You can set the preferences at two levels :-

  • Globally for the Plugin : Go to Window → Preferences → DataNucleus Eclipse Plugin and see the options below that

  • For a Project : Go to {your project} → Properties → DataNucleus Eclipse Plugin and select "Enable project-specific properties"

Plugin configuration - General

Firstly open the main plugin preferences page, set the API to be used, and configure the libraries needed by DataNucleus. These are in addition to whatever you already have in your projects CLASSPATH, but to run the DataNucleus Enhancer/SchemaTool you will require the following

  • javax.persistence.jar the JPA API jar

  • datanucleus-core

  • datanucleus-api-jpa providing DataNucleus JPA API implementation

  • datanucleus-rdbms (or other DataNucleus store plugin): for running SchemaTool

  • Datastore driver jar (e.g JDBC) : for running SchemaTool

Below this you can set the location of a configuration file for Log4j to use. This is useful when you want to debug the Enhancer/SchemaTool operations.

jpa eclipse prefs general

Plugin configuration - Enhancer

Open the "Enhancer" page. You have the following settings

  • Input file extensions : the enhancer accepts input defining the classes to be enhanced. With JPA you will typically just specify the "persistence-unit" and list the classes and mapping files in there. You can alternatively specify the suffices of files that define what will be enhanced (e.g "class" for annotated classes, and "xml" for the ORM mapping file defining entities)

  • Verbose : selecting this means you get much more output from the enhancer

  • PersistenceUnit : Name of the persistence unit if enhancing a persistence-unit

jpa eclipse prefs enhancer

Plugin configuration - SchemaTool

Open the "SchemaTool" page. You have the following settings

  • Input file extensions : SchemaTool accepts input defining the classes to have their schema generated. As for the enhancer, you can run this from a "persistence-unit"

  • Verbose : selecting this means you get much more output from SchemaTool

  • PersistenceUnit : Name of the persistence unit if running SchemaTool on a persistence-unit

  • Datastore details : You can either specify the location of a properties file defining the location of your datastore, or you supply the driver name, URL, username and password.

jpa eclipse prefs schematool

Enabling DataNucleus support

First thing to note is that the DataNucleus plugin is for Eclipse "Java project"s only. After having configured the plugin you can now add DataNucleus support on your projects. Simply right-click on your project in Package Explorer and select DataNucleus→"Add DataNucleus Support" from the context menu.

jpa eclipse enable

Defining 'persistence.xml'

You can also use the DataNucleus plugin to generate a persistence.xml. You do this by right-clicking on a package in your project, and selecting the option. The persistence.xml is generated under META-INF for the source folder. Please note that the wizard will overwrite existing files without further notice.

Enhancing the classes

The DataNucleus Eclipse plugin allows you to easily byte-code enhance your classes using the DataNucleus enhancer. Right-click on your project and select "Enable Auto-Enhancement" from the DataNucleus context menu. Now that you have the enhancer set up you can enable enhancement of your classes. The DataNucleus Eclipse plugin currently works by enabling/disabling automatic enhancement as a follow on process for the Eclipse build step. This means that when you enable it, every time Eclipse builds your classes it will then enhance the classes defined by the available mapping files or what is annotated. Thereafter every time that you build your classes the JPA enabled ones will be enhanced. Easy! Messages from the enhancement process will be written to the Eclipse Console. Make sure that you have your Java files in a source folder, and that the binary class files are written elsewhere If everything is set-up right, you should see the output below.

jpa eclipse enhancer

Generating your database schema

Once your classes have been enhanced you are in a position to create the database schema (assuming you will be using a new schema - omit this step if you already have your schema). Click on the project under "Package Explorer" and under "DataNucleus" there is an option "Run SchemaTool". This brings up a panel to define your database location (URL, login, password etc). You enter these details and the schema will be generated.

jpa eclipse schematool

Messages from the SchemaTool process will be written to the Eclipse Console.

Netbeans

This guide is based around Netbeans v7.0

Perhaps the most important step in developing applications with DataNucleus is the enhancement of compiled classes. NetBeans provides a convenient way of integrating this procedure into the build process without the need for any additional tools or plugins. This is possible because NetBeans has native integration with Maven and Ant. Any DataNucleus project based on Maven will open and run as is with Netbeans. No changes are needed in the project nor in Netbeans. When using Ant Class enhancement thus becomes a simple matter of adding a new task to the existing build.xml generated by NetBeans.

This tutorial shows how to integrate DataNucleus with NetBeans 7.0 to simplify the development of JDO applications. Please contribute any updates to this guide that you have since the developers of DataNucleus don’t use Netbeans

Requirements

The following components are required to complete this tutorial successfully:

Maven : Working with a DataNucleus Maven Project

NetBeans has native integration with Maven. If your project builds with Maven then it will also build with NetBeans without any change. Unzip the sample project to $home/DNSamples/datanucleus-samples-jdo-tutorial-3.0

The sample project comes with a pom.xml. When you try to open this project NetBeans will automatically detect it as maven project.

Start Netbeans

starting netbeans

Open Maven Project

open project

Clean Build the project

maven clean build

You will see a success message as below

BUILD SUCCESS

Total time: 5.406s
Finished at: Fri Aug 05 09:23:53 IST 2011
Final Memory: 14M/108M

Run Main Class

maven run main

The results can be seen in the output window

DataNucleus AccessPlatform with JPA

Persisting products
Product and Book have been persisted

Retrieving Extent for Products
>> Book : JRR Tolkien - Lord of the Rings by Tolkien
>> Product : Sony Discman [A standard discman from Sony]

Executing Query for Products with price below 150.00
>> Book : JRR Tolkien - Lord of the Rings by Tolkien

Deleting all products from persistence
Deleted 2 products

End of Tutorial

BUILD SUCCESS
Total time: 6.221s
Finished at: Fri Aug 05 09:27:43 IST 2011
Final Memory: 7M/106M

Setting up NetBeans for DataNucleus ANT use

The first thing to do is to register the DataNucleus components in the Library Manager of NetBeans 7 so that these become available to any project created with the IDE. This involves creating a new library and adding the JAR files to it, as shown in the following screenshots

Open Library Manager

ant library

Add New Library with Name DN3 - Repeat the process for DN3Deps

library manager

Add all the libraries under $home/datanucleus-accessplatform-full-deps-3.0.0-release/lib to DN3. Add all the libraries under $home/datanucleus-accessplatform-full-deps-3.0.0-release/deps to DN3Deps

library manager

Once this is done, the NetBeans 7 will add the JAR files to the classpath whenever the newly-created DN3 and DN3Deps library is selected for a project.

Ant : Setting up a new project

Delete pom.xml from the downloaded samples project. NetBeans treats any folder with pom.xml as a Maven project. Copy the datanucleus.properties to src/java in the downloaded samples project.

Now create a new project from existing sources.

ant new project

Remember to Select Java Project with Existing Source

new project

Click Next and Add Folder. The source folder should point to datanucleus-samples-jdo-tutorial-3.0/src/java

new java project existing sources

Then click Next and Finish

Now we have the sucessfuly created the sample project using Ant

netbeans ide 7

Add the Libraries created in first set to this project. Right-Click on Libraries in the Projects Tab

ant add libraries

Add DN3 library to your project. Also add DN3Deps add library

We also need to add JDBC support to our project. Add this using add "Add jars" option. Right-Click on Libraries in the Projects Tab and select "Add jars"

ant add jar

Add hsqldb

add jar folder

Ant : Enhancing the classes

The enhancement process needs to be defined and integrated into the build process. As stated in the introduction, this requires a simple change to the nbbuild.xml file.

TypeSafe Queries :- Datanucleus generates additional code for supporting Criteria queries. Ensure that "Enable Annotation processing" check box is selected, which is under Project Properties | build | compiling

Click on the Files tab, expand the project tree, then open nbbuild.xml

sample ant netbeans

Override the * -post-compile * task/target with the following Ant instructions.

<target name="-post-compile" depends="init">
    <path id="module.enhancer.classpath">
        <pathelement path="${javac.classpath}"/>
        <pathelement location="${build.classes.dir}"/>
    </path>
    <taskdef name="datanucleusenhancer" classpathref="module.enhancer.classpath" classname="org.datanucleus.enhancer.EnhancerTask"/>
    <echo message="start datanucleusenhancer"/>
    <datanucleusenhancer classpathref="module.enhancer.classpath" dir="${build.classes.dir}" verbose="true">
        <fileset dir="${build.classes.dir}">
            <include name="**/*.class"/>
        </fileset>
    </datanucleusenhancer>
    <echo message="end datanucleusenhancer"/>
</target>

This target is the most convenient for enhancing classes because it occurs just after all classes have been compiled and is called in any case, whether the project is being built, tested or deployed. This ensures that classes are always enhanced.

Ensure that the Compile on Save option is turned off.

Enable Annotation Processing should be turned ON

project properties ant

Ant : Building the project

The project can now be built, with the knowledge that the classes will be enhanced in the process.

ant clean build

Output window will show some thing similar to this

BUILD SUCCESSFUL (total time: 2 seconds)

Run the main class. The output window will show as below

Product and Book have been persisted

Retrieving Extent for Products
>> Book : JRR Tolkien - Lord of the Rings by Tolkien
>> Product : Sony Discman [A standard discman from Sony]

Executing Query for Products with price below 150.00
>> Book : JRR Tolkien - Lord of the Rings by Tolkien

Deleting all products from persistence
Deleted 2 products

End of Tutorial
BUILD SUCCESSFUL (total time: 2 seconds)

Conclusion

This concludes our tutorial on how to integrate DataNucleus with NetBeans 7. As can be seen, thanks to NetBeans project system based on Ant and Native Maven support, development of JDO applications is largely simplified. This tutorial was provided by a user of this software, Kiran Kumar.

Gradle Plugin

Gradle is a build tool that is used in some organisations. DataNucleus does not currently have an official plugin for using Gradle to build DataNucleus projects.

There are the following unofficial Gradle plugins available

Eclipse Dali

The Eclipse Dali project provides general tools for developing JPA applications using the Eclipse IDE. Please consult their documentation for its usage and capabilities.