gpt4 book ai didi

java - 创建名为 'entityManagerFactory' 的 bean 时出错 -> 无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:17 25 4
gpt4 key购买 nike

我的 j2e 应用程序遇到这个问题,但找不到任何解决方案。构建成功,但抛出运行时异常。我尝试了谷歌的很多建议,但都无法解决我的问题。

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

<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" />

持久性.xml

 <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

<class>com.example.j2eeapp.domain.UserEntity</class>

</persistence-unit>

最佳答案

首先,java编译器在编译时不会验证persistence.xml是否存在。该错误仅发生在运行时。

您得到的堆栈跟踪非常清楚,您的程序无法在类路径上找到 persistence.xml。

jar 中 persistence.xml 的位置必须是:META-INF/persistence.xml,或者如果是 war :WEB-INF/classes/META-INF/persistence.xml

关于java - 创建名为 'entityManagerFactory' 的 bean 时出错 -> 无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com