当前位置: 首页 > 知识库问答 >
问题:

Spring DataJpaTest不加载Java9的javax.transaction.SystemException

易祖鹤
2023-03-14

我想用DataJpaTest注释测试我的服务层。我的设置如下:SpringBoot 2.0.0-M7、JAVA 9和pom xml:

<dependencies>
    <!-- Compile -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <!-- Provided -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- Runtime -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>javax.transaction-api</artifactId>
        <version>1.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

这是我真正简单的测试单元:

@RunWith(SpringRunner.class)
@DataJpaTest
public class UserServiceTest
{
    @Autowired
    private UserRepository userRepository;

    @Test
    public void addUser()
    {
        final User dummyUser = ...
        userRepository.save(dummyUser);
    }
}

但我总是遇到同样的问题

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl ... Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException

mvn依赖项:树:

[INFO] --- maven-dependency-plugin:3.0.1:tree (default-cli) @ webapp ---
[INFO] com.webapp:jar:1.0.0-DEV01-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:2.0.0.M7:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.0.M7:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.0.0.M7:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.0.M7:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.0.M7:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.1:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.0.M7:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.2:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.2:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:2.0.0.M7:compile
[INFO] |  |  \- io.projectreactor.ipc:reactor-netty:jar:0.7.2.RELEASE:compile
[INFO] |  |     +- io.netty:netty-codec-http:jar:4.1.17.Final:compile
[INFO] |  |     |  \- io.netty:netty-codec:jar:4.1.17.Final:compile
[INFO] |  |     +- io.netty:netty-handler:jar:4.1.17.Final:compile
[INFO] |  |     |  +- io.netty:netty-buffer:jar:4.1.17.Final:compile
[INFO] |  |     |  \- io.netty:netty-transport:jar:4.1.17.Final:compile
[INFO] |  |     |     \- io.netty:netty-resolver:jar:4.1.17.Final:compile
[INFO] |  |     +- io.netty:netty-handler-proxy:jar:4.1.17.Final:compile
[INFO] |  |     |  \- io.netty:netty-codec-socks:jar:4.1.17.Final:compile
[INFO] |  |     \- io.netty:netty-transport-native-epoll:jar:4.1.17.Final:compile
[INFO] |  |        +- io.netty:netty-common:jar:4.1.17.Final:compile
[INFO] |  |        \- io.netty:netty-transport-native-unix-common:jar:4.1.17.Final:compile
[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.5.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:2.0.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework:spring-web:jar:5.0.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.0.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-webflux:jar:5.0.2.RELEASE:compile
[INFO] |  \- org.synchronoss.cloud:nio-multipart-parser:jar:1.1.0:compile
[INFO] |     +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] |     \- org.synchronoss.cloud:nio-stream-storage:jar:1.1.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.0.0.M7:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.0.0.M7:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:5.0.2.RELEASE:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.13:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.0.M7:compile
[INFO] |  |  +- com.zaxxer:HikariCP:jar:2.7.4:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:5.0.2.RELEASE:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:5.2.12.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  +- org.javassist:javassist:jar:3.22.0-CR2:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  +- org.jboss:jandex:jar:2.0.3.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:5.0.2.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-context:jar:5.0.2.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-expression:jar:5.0.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:5.0.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:5.0.2.RELEASE:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.3.0:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.0.M7:provided
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.23:provided
[INFO] |  |  \- org.apache.tomcat:tomcat-annotations-api:jar:8.5.23:provided
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.23:provided
[INFO] |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.23:provided
[INFO] +- org.postgresql:postgresql:jar:42.1.4:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.0.0.M7:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.0.0.M7:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.0.M7:test
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] |  |  \- net.minidev:json-smart:jar:2.3:test
[INFO] |  |     \- net.minidev:accessors-smart:jar:1.2:test
[INFO] |  |        \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] |  +- junit:junit:jar:4.12:test
[INFO] |  +- org.assertj:assertj-core:jar:3.8.0:test
[INFO] |  +- org.mockito:mockito-core:jar:2.12.0:test
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.7.9:test
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.7.9:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.6:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-core:jar:5.0.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-test:jar:5.0.2.RELEASE:test
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.5.1:test
[INFO] +- io.projectreactor:reactor-test:jar:3.1.2.RELEASE:test
[INFO] |  \- io.projectreactor:reactor-core:jar:3.1.2.RELEASE:compile
[INFO] |     \- org.reactivestreams:reactive-streams:jar:1.0.1:compile
[INFO] +- com.h2database:h2:jar:1.4.196:test
[INFO] \- javax.transaction:javax.transaction-api:jar:1.2:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------    

共有2个答案

程俊力
2023-03-14

正如@Jenschauder指出的,新的Java9模块系统引起了我的问题。最后一个Spring版本似乎还没有准备好在Java9上运行。

为了解决我的问题,我简单地回滚到JAVA 8。如果勇敢的人来到这里,想要使用JAVA 9,那么您必须创建一个模块信息。src文件夹根目录下的java文件。然后列出所需的所有模块:

module myapp.package {
    requires javax.transaction.api;
    requires spring.boot;
    requires spring.boot.autoconfigure;
    ...
}

我放弃了为我的应用程序查找所有必需的模块。

刘承悦
2023-03-14

尝试添加此内容

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.11</version>
    </dependency>
 类似资料:
  • 该表放置在“JScrollPanel”中,并使用“DefaultTableModel”添加标题和行。 无论我做了什么,我都无法使表显示标题或行。 我在这里遗漏了什么?

  • 我正在研究集合的工厂方法。我看到方法有10个varargs重载(与相同)。我真的不明白为什么有这么多。最后,函数

  • 在Java 9中,在列表界面下,有一个新的()方法,根据Java文档: 名单。of()静态工厂方法提供了一种创建不可变列表的方便方法。

  • Java文档可以使用工具生成。 它目前以格式生成文档。 在java 9中,可以通过在命令行参数中使用选项生成格式的文档。 旧式的java文档 考虑F:\worksp\java9文件夹中有一个Java文件:Tester.java,其代码如下 - 可以运行jdk 7 的javadoc工具来生成文档。 使用标志运行jdk 9的javadoc工具以生成新类型的文档。 打开生成的文件,如下所示 -

  • 我需要下载shapely,但我总是出错。 起初,我尝试使用: 它不起作用,所以我在网上搜索它,有人告诉我去这里下载文件:https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely我下载了名为Shapely1.6.4.post1-cp37-cp37mwin_amd64.whl的文件,因为我的窗口是64位,这是最新的一个。我又试了一次,但没有成功。我在这个问

  • 我使用jdk9-ea 149并创建了一个示例javafx应用程序 IntelliJ不理解java库,它用红色显示所有的import语句,它们都是灰色的,甚至对于java.util.List和java.util.ArrayList也有同样的问题。 我能够编译外部表单,但不能编译Intellij2016.3.2终极版的代码。 我正在使用实验特性,但它仍然不理解导入语句-请参见下面的intellij设置