创建名为“entityManagerFactory"的 bean 时出错 ->无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元 [英] Error creating bean with name 'entityManagerFactory' -> Cannot parse persistence unit from class path resource [META-INF/persistence.xml]

查看:40
本文介绍了创建名为“entityManagerFactory"的 bean 时出错 ->无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 j2e 应用程序中有这个问题,我找不到任何解决方案.构建成功但抛出运行时异常.我尝试了 google 的许多建议,但没有任何方法可以解决我的问题.

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.

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

entityManagerFactory in data-source

<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 的存在.错误只发生在运行时.

Firstly, java compiler will not validate the existence of persistence.xml during compile time. The error only happen at runtime.

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

Stack trace you're getting is pretty clear, your program could not locate persistence.xml on classpath.

jar 中persistence.xml 的位置必须是:META-INF/persistence.xml,或者如果是war:WEB-INF/classes/META-INF/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

这篇关于创建名为“entityManagerFactory"的 bean 时出错 ->无法从类路径资源 [META-INF/persistence.xml] 解析持久性单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆