Search code examples
gradleopenjpa

How can I enhance classes for openJPA with gradle


I am stuck setting up a simple test program with - eclipse - gradle (Eclipse Buildship plugin) - openjpa

When I try to run my application I get this error, when I call the entityManager.persist(...) method:

Exception in thread "main" org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance "test.jpa.Person@27c6e487" to PersistenceCapable failed. Ensure that it has been enhanced. FailedObject: test.jpa.Person@27c6e487

The program is simple, it just has one entity (Person.java). My persistence.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="ptest"
        transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <properties>
            <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/ptest" />
            <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
            <property name="openjpa.ConnectionUserName" value="..." />
            <property name="openjpa.ConnectionPassword" value="..." />
            <property name="openjpa.RuntimeUnenhancedClasses" value="unsupported" />
            <property name="openjpa.DynamicEnhancementAgent" value="false" />
            <property name="openjpa.Log" value="SQL=ERROR" />
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
            <property name="openjpa.ConnectionFactoryProperties"
                value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000" />
        </properties>
    </persistence-unit>
</persistence>
I have used RuntimeUnenhancedClasses=unsupported and DynamicEnhancementAgent=false, for it was recommended by apache. But I have tried with "supported" and "true" and I still get the same error (for openjpa version 2.1.0 - for higher versions I get a LinageError with "supported/true").

I have tried different versions of jpa, currently using 2.4.2.

I have goodled for gradle scripts to enhance my class, I tried

  • 'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0'

  • 'neva.openjpa'

and some more, but I just cannot enhance the class.

Of course I have goodled for quite some time. I am thankful for any idea I can follow...

Thanks in advance!


Solution

  • Just in case someone else has the same struggle. I have found a solution to this problem:

    TomEE ships with JPA and everything that is needed. The eclipse "gradle buildship" plugin can be used to compile code. So the combination of the following components works perfectly fine for me:

    • apache-tomee-plus-7.0.2
    • tomcat v8.5 server (pointing to the apache-tommee installation)
    • Dynamic Web Project (using Dynamic Web Module 3.1)
    • Java Runtime Environment v1.8

    The gradle script reduces simply to this:

    apply plugin: 'war'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        providedCompile "javax:javaee-api:7.0"
    }
    

    I suppose it also works with TomEE 1.7.x and javaee-api:6.0, but I have not tried it.