thehpi
thehpi

Reputation: 5833

JPA persistence unit definitions are conflicting

I want to use one class and two JPA persistence units and as such to be able to store data in different tables (or even databases) and different definitions.

According to the JPA2.2 specification this should be possible but I experience weird behaviour. I'm using payara which uses eclipselink.

For a complete description and a reproducer see this github project.

I hope someone can help me.

Upvotes: 2

Views: 494

Answers (1)

thehpi
thehpi

Reputation: 5833

The reason why this does not work is eclipselink weaving. What this does is manipulate the bytecode of entity classes to extend them with functionality so provide all kinds of optimizations.

The problem however is that the resulting 'woven' classes depend on the definitions as defined in the persistence.xml and orm.xml.

This means in my case, because I have two different persistence.xml/orm.xml combinations, I would need two Foo.class and Bar.class files which reflect the different functionality. Of course this won't work.

The solution is to turn off weaving and this can be done using a property in the persistence.xml

<property name="eclipselink.weaving" value="off"/>

If you want to see the actual 'woven' classes you can use static weaving. This can be done using this property

<property name="eclipselink.weaving" value="static"/>

and this maven plugin

<plugin>
    <groupId>de.empulse.eclipselink</groupId>
    <artifactId>staticweave-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
      <execution>
        <phase>process-classes</phase>
        <goals>
          <goal>weave</goal>
        </goals>
        <configuration>
          <persistenceXMLLocation>META-INF/persistence.xml</persistenceXMLLocation>
          <logLevel>FINE</logLevel>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>2.7.7.payara-p3</version>
      </dependency>
    </dependencies>
  </plugin>

When you then build the project you can decompile the classes in the jar to see what is happening.

Thanks you Cris for pointing me to eclipselink weaving.

Upvotes: 2

Related Questions