java 创建名为“entityManagerFactory”的 bean 时出错 -> 无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17819938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error creating bean with name 'entityManagerFactory' -> Cannot parse persistence unit from class path resource [META-INF/persistence.xml]
提问by lukas
I have this problem in my j2e application and I can't find any solution. Build was successful but there is thrown runtime exception. I tried many advices from google but nothing can solve my problem.
我的 j2e 应用程序中有这个问题,我找不到任何解决方案。构建成功,但抛出运行时异常。我尝试了 google 的许多建议,但没有什么能解决我的问题。
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/datasource-config.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot parse persistence unit from class path resource [META-INF/persistence.xml]
entityManagerFactory in data-source
数据源中的 entityManagerFactory
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" >
<property name="connectionCachingEnabled" value="true" />
<property name="URL" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="user" value="postgres" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="default" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
persistence.xml
持久化文件
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<class>com.example.j2eeapp.domain.UserEntity</class>
</persistence-unit>
采纳答案by gerrytan
Firstly, java compiler will not validate the existence of persistence.xml during compile time. The error only happen at runtime.
首先,java编译器在编译时不会验证persistence.xml的存在。错误只发生在运行时。
Stack trace you're getting is pretty clear, your program could not locate persistence.xml on classpath.
您得到的堆栈跟踪非常清楚,您的程序无法在类路径上找到 persistence.xml。
The location of persistence.xml within your jar has to be: META-INF/persistence.xml, or if it's war: WEB-INF/classes/META-INF/persistence.xml
jar 中persistence.xml 的位置必须是:META-INF/persistence.xml,或者如果是war:WEB-INF/classes/META-INF/persistence.xml
回答by Bohdan Myslyvchuk
I've had similar problem, but in my xml-file I've had correct path to persistence.xml. Problem was in that relations between my entities was wrong. Also I've had wrong import for date (I used Spring Data JPA which doesn't use sql.Date)
我遇到了类似的问题,但在我的 xml 文件中,我有正确的持久性.xml 路径。问题在于我的实体之间的关系是错误的。我也有错误的日期导入(我使用了不使用 sql.Date 的 Spring Data JPA)
回答by karthik
1)Please check your datasource class it says oracle.jdbc.pool.OracleDataSource
1)请检查它说的数据源类 oracle.jdbc.pool.OracleDataSource
2)Check your db details- username, password, driver URL
2) 检查您的数据库详细信息 - 用户名、密码、驱动程序 URL
If the above details are correct.
如果上面的细节是正确的。
3)Create a database in the back-end and try to refer it. And make sure all your tables and entities that your code is referring, are present.
3)在后端创建一个数据库并尝试引用它。并确保您的代码所引用的所有表和实体都存在。
4)clean your tomcat(web server) and try to restart your application
4)清理您的tomcat(网络服务器)并尝试重新启动您的应用程序
I had the same issue. Resolved it using the above steps. Hope this helps !!!
我遇到过同样的问题。按照上面的步骤解决了。希望这可以帮助 !!!