1

我在我的 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>
4

3 回答 3

1

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

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

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

于 2013-07-23T23:00:07.093 回答
0

我有类似的问题,但在我的 xml 文件中,我有到 persistence.xml 的正确路径。问题在于我的实体之间的关系是错误的。另外我的日期导入错误(我使用了不使用 sql.Date 的 Spring Data JPA)

于 2015-01-10T11:15:42.893 回答
0

1)请检查它说的你的数据源类oracle.jdbc.pool.OracleDataSource

2)检查您的数据库详细信息 - 用户名、密码、驱动程序 URL

如果上面的细节是正确的。

3)在后端创建一个数据库并尝试引用它。并确保您的代码所引用的所有表和实体都存在。

4)清理你的tomcat(网络服务器)并尝试重新启动你的应用程序

我遇到过同样的问题。使用上述步骤解决了它。希望这可以帮助 !!!

于 2016-11-28T10:32:43.480 回答