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

Spring Boot 1.4.1可运行jar javax.persistence.persistenceException:无法解析持久性单位根URL

锺离刚洁
2023-03-14

我们有一个Spring Boot1.4.1应用程序,当我们创建可运行的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数据JPA存储库。

共有1个答案

扈翰
2023-03-14

根据我在这里读到的说明(https://github.com/spring-projects/spring-boot/issues/6983#issuecomment-249981782),我通过以下操作创建了该问题的解决方案:

>

  • 我创建了meta-inf/orm.xml文件,并将其放在src/main/resources文件夹中。文件的内容可以在以下链接中找到:https://github.com/spring-projects/spring-data-jpa/blob/master/src/test/resources/meta-inf/orm.XML(实际上不需要persistence-unit-metadata、named-query和entity标记,只需要一个带有根entity-mappings标记的空XML就够了)。

    在配置LocalContainerEntityManagerFactoryBean bean时,我需要调用setPersistenceUnitRootLocation方法,并将location设置为classpath:meta-inf/orm.xml

    这个变通解决了我的问题时运行脂肪罐。但是,通过实现这个解决办法,我产生了另一个问题--我无法从IDE运行一个应用程序,所以我必须基于某种指示符实现持久性单元的根位置的某种条件设置,不管我是从jar还是从IDE运行。

  •  类似资料:
    • ConfigEmbeddedWebApplicationContext:上下文初始化过程中遇到异常-取消刷新att empt:org.springframework.beans.factory.beanCreationException:创建类路径资源[org/springframework/boot/autoconfiguration/orm/jpa/hibernatejpaAutoConfig

    • 我正在开发Hibernate JPA持久性Web应用程序,我的persistence.xml在src/main/Resources/META-INF/persistence.xml(在war文件中,它在WEB-INF/class/META-INF中)。这一切在本地Tomcat服务器上运行良好,但当放在Openshift JBoss EWS上时,在启动时收到此消息: 坚持不懈xml以 并且有两个持久

    • 我的项目具有以下结构: 请告诉我--我完全不相信日志的建议,即与我的JAR中的实体和DAO有关的persistence.xml文件实际上应该托管在我的WAR中。

    • 使用Spring Core 3.0.5,Hibernate 3.6.0。这个问题的标题是在启动Tomcat网络服务器(6.0.29)时使用我正在维护的应用程序时提出的java.lang.IllegalStateExcture的消息。 应用程序上下文。xml表示: 坚持不懈xml位于项目包含的jar文件的META-INF目录中。驱逐异见者。网状物war有一个WEB-INF/classes文件夹,其中

    • 问题内容: 我有我的配置: 我得到错误 我知道这可能缺少属性文件,但是我在类路径中恰好有它。有什么不见了? 我的web.xml: 问题答案: 你的应用程序中可能有多个。尝试在超类的方法上设置一个断点,看看在应用程序启动时是否多次调用了该断点。如果不止一个,则可能需要查看配置属性,以便你的应用程序可以正常启动。

    • 我已经进入了几天,尽管我学到了很多,但我开始绝望了。 我已经尝试了这个极好问题的所有建议: 没有名为的EntityManager的持久性提供程序 我曾经使用无处不在的HibernateUtil类来处理这个问题,但是被告知要在这里转移到一个简单的JPA风格: Spring-RESTful控制器方法改进建议 不幸的是,我无法让豆子注射在spring boot中正常工作。以下是我的尝试: Spring