Hello All,
I have a requirement of using multiple persistence.xml file in more than one module (jars) inside a web application (war). I need to merge all the persistence.xml file with the same persistence context name and create and use the EntityManager out of them.
I searched many places in google and found few links but couldn't found any working code example with JPA 2.0 + Spring 3.0.5.RELEASE + Hibernate 3.5.1.
I am using below artifacts:
All required spring dependencies with version 3.0.5.RELEASE
Below are the spring and java code details am using:Code:<dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.7.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.7.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.5.6-Final</version> </dependency>
spring applicationContext.xml
MergePersistenceUnitManager.javaCode:<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitManager" ref="mergedPersistenceUnitManager" /> <!-- other details are excluded-->... ... ... </bean> <bean id="mergedPersistenceUnitManager" class="com.package.name.MergePersistenceUnitManager"> <property name="persistenceXmlLocations"> <list> <value>classpath*:META-INF/persistence.xml</value> </list> </property> <property name="defaultDataSource" ref="dataSource" /> </bean>
from https://github.com/SpringSource/spri...itManager.java
MergePersistenceUnitManager.javaCode:public class MergePersistenceUnitManager extends DefaultPersistenceUnitManager { @Override protected void postProcessPersistenceUnitInfo(final MutablePersistenceUnitInfo mutablePersistenceUnitInfo) { super.postProcessPersistenceUnitInfo(mutablePersistenceUnitInfo); final PersistenceUnitInfo oldPersistenceUnitInfo = getPersistenceUnitInfo(mutablePersistenceUnitInfo.getPersistenceUnitName()); if (oldPersistenceUnitInfo != null) { for (URL url : oldPersistenceUnitInfo.getJarFileUrls()) { if (!mutablePersistenceUnitInfo.getJarFileUrls().contains(url)) { mutablePersistenceUnitInfo.addJarFileUrl(url); } } mutablePersistenceUnitInfo.addJarFileUrl(oldPersistenceUnitInfo.getPersistenceUnitRootUrl()); } } }
from https://dev.c-ware.de/confluence/dis...le+application
persitence.xml in module one (JAR-1) inside META-INFCode:public class MergePersistenceUnitManager extends DefaultPersistenceUnitManager { @Override protected void postProcessPersistenceUnitInfo(final MutablePersistenceUnitInfo newPU) { super.postProcessPersistenceUnitInfo(newPU); final URL persistenceUnitRootUrl = newPU.getPersistenceUnitRootUrl(); newPU.addJarFileUrl(persistenceUnitRootUrl); final String persistenceUnitName = newPU.getPersistenceUnitName(); final MutablePersistenceUnitInfo oldPU = getPersistenceUnitInfo(persistenceUnitName); if (oldPU != null) { final List<URL> urls = oldPU.getJarFileUrls(); for (URL url : urls){ newPU.addJarFileUrl(url); } final List<String> managedClassNames = oldPU.getManagedClassNames(); for (String managedClassName : managedClassNames){ newPU.addManagedClassName(managedClassName); } final List<String> mappingFileNames = oldPU.getMappingFileNames(); for (String mappingFileName : mappingFileNames){ newPU.addMappingFileName(mappingFileName); } final Properties oldProperties = oldPU.getProperties(); final Properties newProperties = newPU.getProperties(); newProperties.putAll(oldProperties); newPU.setProperties(newProperties); } } }
persitence.xml in module one (JAR-2) inside META-INFCode:<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_2_0.xsd" version="2.0"> <persistence-unit name="PUUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> <class>com.package.name.UserEntity</class> <exclude-unlisted-classes/> <properties> ... ... ... .... </properties> </persistence-unit> </persistence>
When I am running this setup either from Junit or by deployement I am getting below error, it seems problem with Hibernate API while parsing multiple persistence.xml.Code:<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_2_0.xsd" version="2.0"> <persistence-unit name="PUUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> <class>com.package.name.Employee</class> <class>com.package.name.Department</class> <exclude-unlisted-classes/> <properties> ... ... ... .... </properties> </persistence-unit> </persistence>
Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: error trying to scan <jar-file>: file:/D:/ProjectWorks/jpa_project/trunk/target/test-classes/
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean
......
Caused by: java.lang.RuntimeException: error trying to scan <jar-file>: file:/D:/ProjectWorks/DisneySvn/rti/framework/persistence/trunk/target/test-classes/
at org.hibernate.ejb.Ejb3Configuration.scanForClasses (Ejb3Configuration.java:854)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3 Configuration.java:594)
at org.hibernate.ejb.HibernatePersistence.createConta inerEntityManagerFactory(HibernatePersistence.java :73)
at org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean.createNativeEntityManagerFactory( LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerF actoryBean.afterPropertiesSet
.....
Could anyone please help me out on this with any working code examples. I am stuck with this issue and it's a show stopper for my project.
Many thanks in advance.


Reply With Quote