使用Java 11的Spring Boot 2.1:无法解析持久性单元根URL

问题描述 投票:2回答:1

我有一个应用程序,它使用Spring Boot和Spring Data JPA,并为其持久层提供基于注释的配置。我已经开始将此应用程序与Java(OpenJDK)一起迁移到最新的Spring Boot版本(2.1.x).11。配置模块描述符后,应用程序启动,但是当Spring到达想要构建的时候持久层,应用程序停止,出现以下异常:

Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
    at [email protected]/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:640)
    at [email protected]/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:462)
    at [email protected]/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:443)
    at [email protected]/org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:328)
    at [email protected]/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
    at [email protected]/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
    ... 16 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
    at [email protected]/org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195)
    at [email protected]/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:636)
    ... 21 common frames omitted

只有在我尝试在IntelliJ IDEA中运行应用程序时才会出现此问题,IntelliJ IDEA通过配置模块路径而不是类路径(如预期)来启动JVM。当我尝试运行打包为可执行jar的应用程序时,问题就消失了,但正如我所注意到的,当应用程序从可执行jar运行时,模块描述符基本上被忽略了......

我设法发现的是org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager找到实体,并将它们存储在持久性单元中,但之后,它会尝试确定持久性单元根URL,这就是发生故障的点。如果没有模块描述符,应用程序就有一个类路径,这对于DefaultPersistenceUnitManager来说已经足够了,它将使用目标目录(从IDEA运行)或持久性Maven模块的JAR(从可执行JAR运行)。

我假设模块描述符中缺少一些东西,所以仅供参考,它看起来像这样:

open module leaflet.app.backend.persistence {
    requires java.persistence;
    requires java.validation;
    requires org.apache.commons.lang3;
    requires org.hibernate.orm.core;
    requires spring.beans;
    requires spring.context;
    requires spring.data.commons;
    requires spring.data.jpa;
    requires spring.tx;

    exports hu.psprog.leaflet.persistence.dao;
    exports hu.psprog.leaflet.persistence.entity;
    exports hu.psprog.leaflet.persistence.repository;
    exports hu.psprog.leaflet.persistence.repository.specification;
}

如果有人对这个问题有所了解,请不要犹豫,写下来 - 我基本上都停留在这一点上。非常感谢你提前!

java spring-boot spring-data-jpa java-11 jigsaw
1个回答
3
投票

我有同样的错误,我设法通过添加java.xml.bind来解决它

requires java.persistence;
requires java.validation;
requires java.sql;
requires java.xml.bind;

requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.beans;
requires spring.context;
requires spring.core;
requires spring.data.jpa;
requires spring.data.commons;
requires spring.tx;
requires spring.web;

requires slf4j.api;
requires lombok;
requires net.bytebuddy;
requires tomcat.embed.core;
© www.soinside.com 2019 - 2024. All rights reserved.