Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Last active April 28, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojavelinux/0b8366152dd082a3f9ce2df853e4df3a to your computer and use it in GitHub Desktop.
Save mojavelinux/0b8366152dd082a3f9ce2df853e4df3a to your computer and use it in GitHub Desktop.
Instructions for how to switch to DataNucleus as the JPA provider when testing with or deploying to Apache TomEE.

Using DataNucleus as the JPA provider in Apache TomEE

DataNucleus can be used as an alternate JPA provider in TomEE. This document describes how to make this switch. These instructions assume you are using container-managed JPA. For context, see the project at https://github.com/mojavelinux/apache-tomee-datanucleus-test.

For the Arquillian TomEE Embedded adapter and the DataNucleus entity enhancer, add the following provided-scope dependencies to the main <dependencies> element:

<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-core</artifactId>
    <version>5.0.7</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-api-jpa</artifactId>
    <version>5.0.8</version>
    <scope>provided</scope>
</dependency>

Also add the following test-scoped dependency to the arquillian=tomee-embedded profile:

<dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-rdbms</artifactId>
    <version>5.0.7</version>
    <scope>test</scope>
</dependency>

DataNucleus needs to enhance the entity classes. This step can be done at compile time using a Maven plugin. To configure the DataNucleus Maven plugin to enhance the entity classes, add the following plugin declaration to the plugins under the main <build> element:

<plugin>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-maven-plugin</artifactId>
    <version>5.0.2</version>
    <configuration>
        <api>JPA</api>
        <persistenceUnitName>SecretAgentPU</persistenceUnitName>
    </configuration>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Update the name of the persistence unit in this configuration to match the value of the persistence unit defined in persistence.xml.
📎
The alternative approach is runtime enhancement, which is performed using a javaagent.

To add DataNucleus to the standalone TomEE instance, which is used by the TomEE managed adapter, add the following entries to the <libs> element inside the <configuration> element of the tomee-maven-plugin:

<lib>org.datanucleus:datanucleus-core:5.0.7</lib>
<lib>org.datanucleus:datanucleus-api-jpa:5.0.8</lib>
<lib>org.datanucleus:datanucleus-rdbms:5.0.7</lib>

Run the tomee:build goal to rebuild the TomEE instance:

$ mvn tomee:build -Dtomee-plugin.skipCurrentProject=true

Now that the requisite dependencies and enhancer plugin have been added, configure DataNucleus as the JPA provider in the persistence descriptor (i.e., persistence.xml):

<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>

Also add the following property to the persistence descriptor:

<!-- prevent DataNucleus from messing with the case we use in the mappings -->
<property name="datanucleus.identifier.case" value="MixedCase"/>
⚠️
When using DataNucleus, PersistenceUtilUtil#isLoaded(Object, String) forces a lazy association to load instead of checking whether it’s already loaded.

To enable SQL logging, you need to add the following setting to your logging.properties file:

DataNucleus.Datastore.level = FINE

And away you go!

@andyjefferson
Copy link

The warning about PersistenceUnitUtil.isLoaded is not relevant if using org.datanucleus:datanucleus-api-jpa:5.0.9 or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment