Hibernate、Java 9 和 SystemException [英] Hibernate, Java 9 and SystemException

查看:16
本文介绍了Hibernate、Java 9 和 SystemException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 Java 9/Spring Boot 1.5.x/Maven 项目中运行 Hibernate 5.2.11 应用程序,但我在缺少类时失败:

I've been trying to run Hibernate 5.2.11 application in Java 9/Spring Boot 1.5.x/Maven project but I'm failing at missing class:

Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)
    at org.jboss.logging.Logger$1.run(Logger.java:2554)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
    at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
    at org.hibernate.internal.HEMLogging.messageLogger(HEMLogging.java:28)
    at org.hibernate.internal.HEMLogging.messageLogger(HEMLogging.java:24)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<clinit>(EntityManagerFactoryBuilderImpl.java:115)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 33 more

有没有人遇到过这个异常并且知道解决方法?我尝试为 javax.bind 或 java.se.ee 添加 --add-modules 但它们没有帮助.

Has anyone encountered this exception and knows a workaround? I tried adding --add-modules for javax.bind or java.se.ee but they didn't help.

上述错误显示在使用 Hibernate 启动 Spring 上下文的 mavan-failsafe (2.20.1) 集成测试中.应用程序没有任何 Java 9 特定代码.

The above error shows in a mavan-failsafe (2.20.1) integration test that starts Spring context with Hibernate. Application doesn't have any Java 9 specific code.

推荐答案

根据迁移指南 和 java 文档,因为模块 java.transaction 导出包 javax.transaction 已被标记为 @Deprecated.

According to the migration guide and the java docs, since the module java.transaction which exports the package javax.transaction has been marked as @Deprecated.

理想情况下,您应该将代码迁移为使用 javaee/javax.transaction.目前,您可以使用从依赖项转换而来的自动模块:

You should ideally migrate your code to be using javaee/javax.transaction instead. Currently, you can do so using automatic module converted from the dependency:

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>javax.transaction-api</artifactId>
    <version>1.2</version>
</dependency>

并将以下内容添加到module-info.java:-

and adding to the module-info.java the following:-

requires javax.transaction.api;

<小时>

此外,在使用maven-failsafe-plugin 时,请确保您使用的是Maven 进度文档.


Additionally while using the maven-failsafe-plugin, make sure you are using the minimum compatible version 2.20.1 or above as mentioned in the progress document of Maven.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.20.1</version>
</plugin>

<小时>

@Deprecated(forRemoval="在 OP 确认后")

另一方面,临时解决方法(因为最终这些模块将从 JDK 中删除)可以使用:-

On the other hand, a temporary workaround (since eventually these modules will be removed from the JDK) could be to make use of:-

--add-modules java.transaction

如评论中所述,由于 javax.transaction-api 所需的依赖项已经在类路径上可用,因此您不需要添加任何编译器选项,否则最终使用 java.transaction 模块导出的 javax.transaction 包覆盖当前包,该包最适合您的用例不包含 SystemException.

As mentioned in the comments, since the required dependency for javax.transaction-api is already available on the classpath, you shouldn't be required to add any compiler option or else you would end up overriding the current package with the java.transaction module's exported javax.transaction package which ideally for your use case doesn't consist of SystemException.

这篇关于Hibernate、Java 9 和 SystemException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆