EJB3 JPA error when migrating from JBoss version 4 to 5
When I tried migrating my JBoss 4.2 [war] application which uses Spring to manage transactions, to JBoss 5.1, I got the following error message.
The error message
11:18:55,683 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=#demo state=Create java.lang.RuntimeException: Specification violation [EJB3 JPA 6.2.1.2] - You have not defined a jta-data-source for a JTA enabled persistence context named: demo at org.jboss.jpa.deployment.PersistenceUnitInfoImpl.<init>(PersistenceUnitInfoImpl.java:115) at org.jboss.jpa.deployment.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:275)
Here’s the persistence.xml file
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="demo"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>demo.jpa.User</class> <class>demo.jpa.Issue</class> <class>demo.jpa.UploadedFile</class> <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="true" /> </properties> </persistence-unit> </persistence>
Solution/workaround
JBoss loads file named “persistence.xml” through this file:
jboss-5.1.0.GA/server/default/deployers/ejb3.deployer/META-INF/jpa-deployers-jboss-beans.xml
… via this code snippet (line 21-95)
<bean name="PersistenceParsingDeployer" class="org.jboss.jpa.deployers.PersistenceParsingDeployer"/> <bean name="PersistenceDeployer" class="org.jboss.jpa.deployers.PersistenceDeployer"/> <bean name="PersistenceUnitDeployer" class="org.jboss.jpa.deployers.PersistenceUnitDeployer"> ... </bean>
Solution 1:
Comment out the above lines. This solution is a server configuration modification and therefore must be done on every server (dev/qa/production) that you want to run your app on. You may not be able to use this approach if you don’t have control over your [production] server configuration. See Solution 2 for an application oriented solution.
Solution 2:
- Rename the persistence.xml file to something else, for example, spring-persistence.xml
- Change the config for the spring entityManagerFactory bean to explicitly refer to this new file. See below
Spring configuration – before:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> </bean>
Spring configuration – after:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> <!-- ADD THE FOLLOWING PROPERTY --> <!-- provide the location of new persistence.xml --> <property name="persistenceXmlLocation" value="classpath:META-INF/spring-persistence.xml" /> </bean>
Reference
See more details of the corresponding JIRA issue at jboss.org
<bean name=”PersistenceDeployer” class=”org.jboss.jpa.deployers.PersistenceDeployer”/>
<bean name=”PersistenceUnitDeployer” class=”org.jboss.jpa.deployers.PersistenceUnitDeployer”>
Related posts: