Hibernate + Java 9 + javax.transaction.SystemException

时间:2018-03-31 08:38:30

标签: hibernate java-9

我有同样的问题: Hibernate, Java 9 and SystemException

我可以按照这样的步骤进行操作

    <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>

+ 模块信息:

requires javax.transaction.api;

但该解决方案对我不起作用,因为我仍然遇到错误

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

然后,浏览我发现版本1.3删除了javax.transaction.xa,所以我会去:

    <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>
  • 模块-信息:

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

现在我能够成功构建我的JAR。我正在将该jar导入第二个项目,然后项目崩溃如下:

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)

我已经尝试过: - 改为导入java.se.ee - 将我的JDK从9.0.1更新到9.0.4 - 将我的JDK更新到10.0.0,这让我遇到了maven的问题,所以我离开了那条路线......如果有必要,我可以把它拿回来。 - 我没有使用故障安全...虽然我尝试了StackOverflow关于添加maven插件的答案:P(在这一点上我是否愿意尝试我合乎逻辑的东西)。

有人真的解决了这个问题吗?我的意思是在一个模块中使用hibernate并在第二个模块中使用它来命名模块?

非常感谢

2 个答案:

答案 0 :(得分:2)

好,这需要一点时间 这也适用于JDK 10,注释处理适用于JPAModelGen支持。

配置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>

然后为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>

然后添加java.xml.bind和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>

属性是

<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>

为了确保Maven已下载,我在最上面添加了依赖项

<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>

但是等等,还有更多...

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;

然后相应地配置运行时,eclipse和netbeans只需为上面定义的补丁模块添加vm args。 enter image description here

我在这里使用@args只是为了配合JDK 9及更高版本,因此严格将其模块化。 JDK 11(到目前为止)都是必需的,但补丁程序模块规范可以删除,因为1.3将仅声明该软件包

答案 1 :(得分:0)

更新2019/08/05

库已更新,可以提供更好的支持。 请使用与

相同的模块信息定义
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大大简化了

<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>