5

I kind of have the same problem as: Hibernate, Java 9 and SystemException

I can follow the steps like this

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>javax.transaction-api</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.0.CR1</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.spec.javax.transaction</groupId>
                <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

+ Module-info:

requires javax.transaction.api;

But the solution does not work for me because I'm still getting the errors

[ERROR] error: module org.apache.commons.lang3 reads package javax.transaction.xa from both javax.transaction.api and java.sql

Then, browsing around I found that version 1.3 has the javax.transaction.xa removed, so I'll go:

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>javax.transaction-api</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.0.CR1</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.spec.javax.transaction</groupId>
                <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
  • Module-Info:

    requires java.transaction;
    requires java.persistence;
    requires java.sql;
    requires java.naming;
    requires jboss.logging;
    

Now I'm able to succesfully build my JAR. I'm importing that jar to a second project and then the project crashes like this:

NoClassDefFoundErrorjavax/transaction/SystemException
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:374)
at jboss.logging@3.3.1.Final/org.jboss.logging.Logger$1.run(Logger.java:2554)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at jboss.logging@3.3.1.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at jboss.logging@3.3.1.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.<clinit>(ClassLoaderServiceImpl.java:40)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:218)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:58)
at a.data/com.a.data.hibernate.HibernateServiceLocator.<clinit>(HibernateServiceLocator.java:39)
at g.editor/com.g.App.start(App.java:293)

Caused by javax.transaction.SystemException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:374)
at jboss.logging@3.3.1.Final/org.jboss.logging.Logger$1.run(Logger.java:2554)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at jboss.logging@3.3.1.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at jboss.logging@3.3.1.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.<clinit>(ClassLoaderServiceImpl.java:40)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:218)
at org.hibernate.orm.core@5.3.0.CR1/org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:58)

I already tried to: - import java.se.ee instead - update my JDK from 9.0.1 to 9.0.4 - update my JDK to 10.0.0 which got me into problems with maven, so I left that route... I can take it back if necessary. - I'm not using failsafe... though I tried the StackOverflow answer about adding a maven plugin :P (yep at this point I'm willing to try I logical stuff).

Has anybody actually solved this scenario? I mean hibernate in one module and using it in the second one with named modules?

Thank you so much

Vijay S
  • 282
  • 2
  • 7
  • 15
Chuck
  • 235
  • 3
  • 9
  • Also tried updating Hibernate from 5.2.12.Final to 5.3.0.CR1 – Chuck Mar 31 '18 at 08:39
  • Shall I try another ORM? I'm not terribly exited to do so, but... if there is an ORM solution that works very well with java 9.... – Chuck Mar 31 '18 at 08:41
  • Hibernate providad Java9 examples are useless. – Chuck Mar 31 '18 at 08:42
  • In the section with these lines... *Then, browsing around I found that version 1.3 has the `javax.transaction.xa` removed, so I'll go* , your module definition includes `requires java.transaction` seems to be the cause to me for this behaviour. Reason being - If I search for a class [`javax.transaction.SystemException`](https://search.maven.org/#search%7Cga%7C1%7Cfc%3A%22javax.transaction.SystemException%22) on maven central, 1.3 turns up as the first result. On the other hand, I can certainly see the `java.transaction` module within the JDK doesn't have this class packaged. – Naman Mar 31 '18 at 09:27
  • Do you need the module-info.java files or are those there only to fix your current problem? If you don't need those for any other purpose, then remove them and use only the maven dependency. In both your projects. – Krzysztof Krasoń Mar 31 '18 at 17:41
  • Yeah 1.3 removed the xa folder, so it will not clash with the jdk java.sql... but the exception class is no longer there as well. – Chuck Apr 01 '18 at 03:01
  • Well Krzysz, I have like 15 projects that merge in a war app. I could just ignore deep encapsulation but this code is very well crafted and I'm guessing that there is people out there more brilliant than I'm that should have well distributed, deep encapsulated codeand have solved this problem before. – Chuck Apr 01 '18 at 03:04

2 Answers2

2

Ok, So it takes a bit This is also for JDK 10, The annotation processing is for JPAModelGen support.

Configure maven

<properties>
<jdk.version>1.10</jdk.version>
<jar.classifier>jre10</jar.classifier> <!-- Optional for Dual Build-->
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
<maven.compiler.release>10</maven.compiler.release>

Then configure the compiler for JDK 10

<plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>${maven.compiler.version}</version>
                        <configuration>
                            <release>10</release>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.ow2.asm</groupId>
                                <artifactId>asm</artifactId>
                                <version>6.2</version> <!-- Use newer version of ASM -->
                            </dependency>
                        </dependencies>
                    </plugin>

Then add the patch modules for java.xml.bind and java.transaction

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>${maven.hibernate.version}</version>
                    </annotationProcessorPath>
                    <annotationProcessorPath>
                        <groupId>javax.xml.bind</groupId>
                        <artifactId>jaxb-api</artifactId>
                        <version>${jaxb.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
                <annotationProcessors>
                    <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
                </annotationProcessors>
                <compilerArgs>
                    <arg>-AaddGeneratedAnnotation=false</arg>
                    <arg>--patch-module</arg>
                    <arg>java.transaction=${settings.localRepository}/javax/transaction/javax.transaction-api/${javax.transaction.transactionapi}/javax.transaction-api-${javax.transaction.transactionapi}.jar</arg>
                    <arg>--patch-module</arg>
                    <arg>java.xml.bind=${settings.localRepository}/javax/xml/bind/jaxb-api/${jaxb.version}/jaxb-api-${jaxb.version}.jar</arg>
                </compilerArgs>
                <compilerArguments>
                    <AaddGeneratedAnnotation>false</AaddGeneratedAnnotation>
                    <Adebug>true</Adebug>
                </compilerArguments>
                <failOnError>true</failOnError>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>
                    <version>${maven.hibernate.version}</version>
                    <optional>true</optional>
                </dependency>
                <dependency>
                    <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                    <version>${jaxb.version}</version>
                    <type>jar</type>
                </dependency>
            </dependencies>
        </plugin>

Properties are

<jaxb.version>2.3.0</jaxb.version>
<javax.transaction.transactionapi>1.3</javax.transaction.transactionapi>
<maven.hibernate.version>5.3.2.Final</maven.hibernate.version>
<maven.hibernate.validator.version>6.0.10.Final</maven.hibernate.validator.version>
<maven.hibernate.annotations.version>5.0.1.Final</maven.hibernate.annotations.version>
<maven.hibernate.jpa.persistence.version>1.0.2.Final</maven.hibernate.jpa.persistence.version>

Dependencies on top that i included to make sure maven downloaded

<dependency>
     <groupId>javax.transaction</groupId>
     <artifactId>javax.transaction-api</artifactId>
     <version>${javax.transaction.transactionapi}</version>
     <type>jar</type>
     <scope>provided</provided>
</dependency>


<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${maven.hibernate.version}</version>
    <type>jar</type>
    <exclusions>
        <exclusion>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            <groupId>org.jboss.spec.javax.transaction</groupId>
        </exclusion>
        <exclusion>
            <artifactId>javax.activation-api</artifactId>
            <groupId>javax.activation</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>${maven.hibernate.validator.version}</version>
    <type>jar</type>
    <exclusions>
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

but wait, there's more...

opens xx.xx.xx.entities to org.hibernate.orm.core;

requires java.transaction;
requires java.xml.bind;
requires java.sql;
requires java.activation;
requires java.persistence;

Then configure your runtime accordingly, eclipse and netbeans just add the vm args for the patch modules defined above. enter image description here

I'm using @args here just to go with the grain of JDK 9 and up, so it is strict moduled. JDK 11 (ea so far) all of it is required, except for patch module specifications which can be removed as 1.3 will solely declare the package

Marc Magon
  • 713
  • 7
  • 11
  • Hi I'm Not Using Maven for my Project so can you please explain me how to do all this without maven –  Aug 03 '18 at 22:26
  • 2
    Hi, I would look at documentation for your IDE to implement each step accordingly, Adding in each separate required library will be painful for you, but I strongly *STRONGLY* recommend using a build tool designed for Java, and not so much adapted to it, so Maven Gradle Ivy. Because the steps would be even more than displayed above, and considering it would be specific to your PC configuration, I don't think it is a good idea. Again, I recommend migrating to an actual build tool or creating a question based on your OS, IDE, and requirements – Marc Magon Aug 05 '18 at 03:31
0

Update 2019/08/05

Libraries have been updated to allow for better support. Please use the same module-info definitions as

opens xx.xx.xx.entities to org.hibernate.orm.core;

requires java.transaction;
requires java.xml.bind;
requires java.sql;
requires java.activation;
requires java.persistence;

Maven has been simplified greatly

<plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <annotationProcessorPaths>
                                <annotationProcessorPath>
                                    <groupId>org.hibernate</groupId>
                                    <artifactId>hibernate-jpamodelgen</artifactId>
                                    <version>${maven.hibernate.version}</version>
                                </annotationProcessorPath>
                                <annotationProcessorPath>
                                    <groupId>org.projectlombok</groupId>
                                    <artifactId>lombok</artifactId>
                                    <version>${lombok.version}</version>
                                </annotationProcessorPath>
                                <path>
                                    <groupId>javax.xml.bind</groupId>
                                    <artifactId>jaxb-api</artifactId>
                                    <version>2.3.0</version>
                                </path>
                                <path>
                                    <groupId>javax.annotation</groupId>
                                    <artifactId>javax.annotation-api</artifactId>
                                    <version>1.3.1</version>
                                </path>
                                <path>
                                    <groupId>org.mapstruct</groupId>
                                    <artifactId>mapstruct-processor</artifactId>
                                    <version>${mapstruct.version}</version>
                                </path>
                            </annotationProcessorPaths>
                        </configuration>

                    </plugin>
Marc Magon
  • 713
  • 7
  • 11