首页 > 解决方案 > 在 Java 9 多模块环境中无法解析持久性单元根 URL

问题描述

我正在尝试在 Java 9 中制作一个 Spring Boot Maven 多模块应用程序。它可以在不添加 Spring Data 支持和 @Entity 类的情况下工作,但现在它不会启动。问题是以下异常:

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 javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.context@5.0.9.RELEASE/org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.9.RELEASE.jar:na]
at spring.context@5.0.9.RELEASE/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.9.RELEASE.jar:na]
at spring.context@5.0.9.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:na]
at spring.boot@2.0.5.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:na]
at spring.boot@2.0.5.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:na]
at spring.boot@2.0.5.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:na]
at spring.boot@2.0.5.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:na]
at spring.boot@2.0.5.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:na]
at hu.kleatech.jigsaw/hu.kleatech.jigsaw.MainApplication.main(MainApplication.java:18) [classes/:na]
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
at spring.orm@5.0.9.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:637) ~[spring-orm-5.0.9.RELEASE.jar:na]
at spring.orm@5.0.9.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:459) ~[spring-orm-5.0.9.RELEASE.jar:na]
at spring.orm@5.0.9.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:440) ~[spring-orm-5.0.9.RELEASE.jar:na]
at spring.orm@5.0.9.RELEASE/org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:328) ~[spring-orm-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.9.RELEASE.jar:na]
at spring.beans@5.0.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.9.RELEASE.jar:na]
... 15 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
at spring.core@5.0.9.RELEASE/org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195) ~[spring-core-5.0.9.RELEASE.jar:na]
at spring.orm@5.0.9.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:633) ~[spring-orm-5.0.9.RELEASE.jar:na]
... 20 common frames omitted

互联网上有一些这种例外的例子,但它们都与我无关。我相信多模块 Java 9 环境会以某种方式导致问题,所以这个问题不是其他问题的重复。

我尝试了我能找到和想到的所有可能的解决方案,我什至尝试手动配置所有内容,但它们都没有帮助或让我更接近原因。但是我在这些解决方案中发现了一件有趣的事情:当 Spring Boot 找不到任何 @Entity 类时,似乎会引发此异常。所以我认为 @Entity 类在模块系统中是不可见的。

由于问题似乎与环境有关,因此这是整个项目:(请注意,它处于非常早期的状态,大部分不完整,但至少应该运行) https://github.com/KleaTech/szakdolgozat

任何帮助表示赞赏,我没有想法。

标签: springhibernatespring-data-jpajava-9java-platform-module-system

解决方案


您需要将这些依赖项添加到 pom.xml (或以不同的方式提供它们,如果您不使用 maven):

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>net.bytebuddy</groupId>
    <artifactId>byte-buddy</artifactId>
    <version>1.9.3</version>
</dependency>

并将它们包含在 module-info.java

open module org.demo {
    ...
    requires java.xml.bind;
    requires net.bytebuddy;
}

推荐阅读