Spring Boot 2.1 with Java 11: Unable to resolve persistence unit root URL

南笙酒味 提交于 2019-12-10 16:00:47

问题


I have an application, that uses Spring Boot and Spring Data JPA with annotation-only based configuration for its persistence layer. I've started migrating this application to the latest Spring Boot version (2.1.x) along with Java (OpenJDK) 11. After configuring the module descriptors, the application starts up, but when Spring reaches the point where it wants to build up the persistence layer, the application stops with the following exception:

Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
    at spring.orm@5.1.2.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:640)
    at spring.orm@5.1.2.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:462)
    at spring.orm@5.1.2.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:443)
    at spring.orm@5.1.2.RELEASE/org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:328)
    at spring.beans@5.1.2.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
    at spring.beans@5.1.2.RELEASE/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 spring.core@5.1.2.RELEASE/org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:195)
    at spring.orm@5.1.2.RELEASE/org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:636)
    ... 21 common frames omitted

This issue only happens when I try to run the application in IntelliJ IDEA, which starts the JVM by configuring the module path, instead of the classpath (as expected). When I try to run the application packaged as executable jar, the issue is gone, but as I noticed, the module descriptors are basically ignored when the application runs from the executable jar...

What I've managed to discover is that the org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager finds the entities, and also stores them in a persistence unit, but after that, it tries to determine the persistence unit root URL, and that's the point where the failure occurs. Without the module descriptors, the application has a classpath, and that's enough for the DefaultPersistenceUnitManager, it will use the target directory (running from IDEA) or the persistence Maven module's JAR (running from executable JAR).

I assume there's something I'm missing from the module descriptor, so just for the reference, it looks like this:

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

If anyone might have something in mind about this issue, please don't hesisate to write it down - I'm basically stuck at this point. Thank you very much in advance!


回答1:


I had the same error and I've managed to resolve it by adding 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;



回答2:


Add following into your module-info.java:

requires net.bytebuddy;


来源:https://stackoverflow.com/questions/53572630/spring-boot-2-1-with-java-11-unable-to-resolve-persistence-unit-root-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!