lukas lukas - 1 year ago 176
Java Question

Error creating bean with name 'entityManagerFactory' -> Cannot parse persistence unit from class path resource [META-INF/persistence.xml]

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 in data-source

<beans xmlns=""
xmlns:xsi="" xmlns:tx=""

<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 id="entityManagerFactory"
<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" />
<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" />

<tx:annotation-driven transaction-manager="transactionManager" />

class="" />


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



Answer Source

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

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download