Spring Boot 1.4.1 runnable jar javax.persistence.PersistenceException:无法解析持久性单元根URL

时间:2016-09-26 08:56:34

标签: spring-boot spring-data-jpa

我们有一个Spring Boot 1.4.1应用程序,当我们创建runnable jar并尝试运行它时,我们得到以下堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxEntityManager' defined in class path resource [xxx/web/config/XxxConfig.class]: Invocation of init method failed; nested exception is javax.persistence.Persistenc
eException: Unable to resolve persistence unit root URL
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
        at xxx.App.main(App.java:48) [classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_05]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_05]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_05]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_05]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [NES-Web-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [NES-Web-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [NES-Web-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58) [NES-Web-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:605) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:445) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:426) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:325) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        ... 24 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
        at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187) ~[spring-core-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:602) ~[spring-orm-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
        ... 31 common frames omitted

我们也发现了这个问题https://github.com/spring-projects/spring-boot/issues/6635,但根据它,这应该在Spring Boot 1.4.1中修复。

注意:我们的模型类中没有注释,我们使用hbm.xml文件进行映射。但是,我们使用Spring Data JPA存储库。

3 个答案:

答案 0 :(得分:3)

根据我在此处阅读的说明(https://github.com/spring-projects/spring-boot/issues/6983#issuecomment-249981782),我设法通过以下方式创建解决此问题的方法:

  1. 我创建了 META-INF / orm.xml 文件并将其放在src / main / resources文件夹中。文件的内容可以在此链接https://github.com/spring-projects/spring-data-jpa/blob/master/src/test/resources/META-INF/orm.xml找到(您实际上并不需要持久性单元元数据命名查询 entity 标记,只需一个带有 entity-mappings 标记的空XML就足够了。

  2. 配置 LocalContainerEntityManagerFactoryBean bean时,我需要调用 setPersistenceUnitRootLocation 方法并将位置设置为 classpath:META-INF / orm.xml

  3. 此解决方法在运行胖罐时解决了我的问题。但是,通过实现此解决方法,我创建了另一个问题 - 我无法从IDE运行应用程序,因此我必须根据某些指示器实现某种条件设置持久性单元的根位置,以及是否我可以使用它。 m运行jar或从IDE。

答案 1 :(得分:0)

您可能受this regression影响。该问题中提供了一种解决方法。请试一试,等待1.4.2.RELEASE。对不起:(

答案 2 :(得分:0)

我在使用maven-jar-plugin构建jar时出现了同样的错误,但是像下面这样构建jar工作正常:

    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
     </plugins>
    </build>