hibernate - Spring with multiple persistence units defined in persistence.xml -
i trying have multiple persistence units different databases in spring application, without success far. have tried different scenarios, no 1 has helped, post initial code think should work:
meta/persistence.xml
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="di-main-pu" transaction-type="resource_local"> <description>persistence unit main db (with stores) </description> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <non-jta-data-source>java:comp/env/jdbc/dimaindb</non-jta-data-source> <class>com.mydomainimport.entity.main.corestore</class> <class>com.mydomainimport.entity.main.importresult</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <!-- <property name="javax.persistence.schema-generation.database.action" value="create"/> --> <property name="hibernate.dialect" value="org.hibernate.dialect.mysqldialect"/> <!-- <property name="hibernate.hbm2ddl.auto" value="validate"/> --> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> <!-- jdbc/distorehvt --> <persistence-unit name="di-storehvt-pu" transaction-type="resource_local"> <description>persistence unit entities imported fromxml files</description> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <non-jta-data-source>java:comp/env/jdbc/distorehvt</non-jta-data-source> <class>com.mydomainimport.entity.ano.anocustomer</class> <class>com.mydomainimport.entity.corecustomer</class> <class>com.mydomainimport.entity.corestate</class> <class>com.mydomainimport.entity.rev.revinfo</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.mysqldialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> </persistence>
applicationcontext.xml (spring-beans.xml)
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- post-processors standard config annotations --> <context:annotation-config /> <context:component-scan base-package="com.mydomain.import.service" /> <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.defaultpersistenceunitmanager"> <property name="defaultpersistenceunitname" value="di-main-pu"/> <property name="persistencexmllocation" value="classpath*:meta-inf/persistence*.xml"/> </bean> <bean id="emf" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="persistenceunitmanager" ref="pum"/> </bean> <bean id="importservice" class="com.mydomain.import.service.importserviceimpl"> </bean> </beans>
importserviceimpl:
public class importserviceimpl implements importservice { public static final string main_pu_name = "di-main-pu"; @persistencecontext(unitname=main_pu_name) private entitymanager mainem; @override public entitymanager getstoreentitymanager(integer storeid) { string puname = "di-storehvt-pu"; entitymanagerfactory emf = persistence.createentitymanagerfactory(puname); return emf.createentitymanager(); } }
the exception junit test is:
javax.persistence.persistenceexception: unable build entity manager factory @ org.hibernate.jpa.hibernatepersistenceprovider.createentitymanagerfactory(hibernatepersistenceprovider.java:83) @ org.hibernate.ejb.hibernatepersistence.createentitymanagerfactory(hibernatepersistence.java:54) @ javax.persistence.persistence.createentitymanagerfactory(persistence.java:55) @ javax.persistence.persistence.createentitymanagerfactory(persistence.java:39) ................................................ caused by: org.hibernate.engine.jndi.jndiexception: error parsing jndi name [java:comp/env/jdbc/distorehvt] @ org.hibernate.engine.jndi.internal.jndiserviceimpl.parsename(jndiserviceimpl.java:141) @ org.hibernate.engine.jndi.internal.jndiserviceimpl.locate(jndiserviceimpl.java:112) @ org.hibernate.engine.jdbc.connections.internal.datasourceconnectionproviderimpl.configure(datasourceconnectionproviderimpl.java:115) @ org.hibernate.boot.registry.internal.standardserviceregistryimpl.configureservice(standardserviceregistryimpl.java:111) @ org.hibernate.service.internal.abstractserviceregistryimpl.initializeservice(abstractserviceregistryimpl.java:234) @ org.hibernate.service.internal.abstractserviceregistryimpl.getservice(abstractserviceregistryimpl.java:206) @ org.hibernate.engine.jdbc.internal.jdbcservicesimpl.buildjdbcconnectionaccess(jdbcservicesimpl.java:260) @ org.hibernate.engine.jdbc.internal.jdbcservicesimpl.configure(jdbcservicesimpl.java:94) @ org.hibernate.boot.registry.internal.standardserviceregistryimpl.configureservice(standardserviceregistryimpl.java:111) @ org.hibernate.service.internal.abstractserviceregistryimpl.initializeservice(abstractserviceregistryimpl.java:234) @ org.hibernate.service.internal.abstractserviceregistryimpl.getservice(abstractserviceregistryimpl.java:206) @ org.hibernate.cfg.configuration.buildtyperegistrations(configuration.java:1885) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1843) @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$4.perform(entitymanagerfactorybuilderimpl.java:850) @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$4.perform(entitymanagerfactorybuilderimpl.java:843) @ org.hibernate.boot.registry.classloading.internal.classloaderserviceimpl.withtccl(classloaderserviceimpl.java:397) @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl.build(entitymanagerfactorybuilderimpl.java:842) @ org.hibernate.jpa.hibernatepersistenceprovider.createentitymanagerfactory(hibernatepersistenceprovider.java:75) ... 35 more caused by: javax.naming.operationnotsupportedexception: simplenamingcontext not support [javax.naming.name] @ org.springframework.mock.jndi.simplenamingcontext.getnameparser(simplenamingcontext.java:259) @ javax.naming.initialcontext.getnameparser(initialcontext.java:499) @ org.hibernate.engine.jndi.internal.jndiserviceimpl.parsename(jndiserviceimpl.java:135) ... 52 more
if try deploy same code on tomcat, following:
javax.persistence.persistenceexception: unable build entity manager factory @ org.hibernate.jpa.hibernatepersistenceprovider.createentitymanagerfactory(hibernatepersistenceprovider.java:83) @ org.hibernate.ejb.hibernatepersistence.createentitymanagerfactory(hibernatepersistence.java:54) @ javax.persistence.persistence.createentitymanagerfactory(persistence.java:55) @ javax.persistence.persistence.createentitymanagerfactory(persistence.java:39) @ com.domain.import.service.store.storeserviceimpl.getstoreentitymanager(storeserviceimpl.java:35) @ com.domain.import.service.importserviceimpl.processcustomerxmlfile(importserviceimpl.java:194) @ com.domain.import.service.scheduler.schedulerserviceimpl.updatefromano(schedulerserviceimpl.java:42) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.springframework.scheduling.support.scheduledmethodrunnable.run(scheduledmethodrunnable.java:64) @ org.springframework.scheduling.support.delegatingerrorhandlingrunnable.run(delegatingerrorhandlingrunnable.java:53) @ org.springframework.scheduling.concurrent.reschedulingrunnable.run(reschedulingrunnable.java:81) @ java.util.concurrent.executors$runnableadapter.call(executors.java:471) @ java.util.concurrent.futuretask.run(futuretask.java:262) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$201(scheduledthreadpoolexecutor.java:178) @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(scheduledthreadpoolexecutor.java:292) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:745) caused by: org.hibernate.engine.jndi.jndiexception: unable lookup jndi name [java:comp/env/jdbc/distorehvt] @ org.hibernate.engine.jndi.internal.jndiserviceimpl.locate(jndiserviceimpl.java:117) @ org.hibernate.engine.jdbc.connections.internal.datasourceconnectionproviderimpl.configure(datasourceconnectionproviderimpl.java:115) @ org.hibernate.boot.registry.internal.standardserviceregistryimpl.configureservice(standardserviceregistryimpl.java:111) @ org.hibernate.service.internal.abstractserviceregistryimpl.initializeservice(abstractserviceregistryimpl.java:234) @ org.hibernate.service.internal.abstractserviceregistryimpl.getservice(abstractserviceregistryimpl.java:206) @ org.hibernate.engine.jdbc.internal.jdbcservicesimpl.buildjdbcconnectionaccess(jdbcservicesimpl.java:260) @ org.hibernate.engine.jdbc.internal.jdbcservicesimpl.configure(jdbcservicesimpl.java:94) @ org.hibernate.boot.registry.internal.standardserviceregistryimpl.configureservice(standardserviceregistryimpl.java:111) @ org.hibernate.service.internal.abstractserviceregistryimpl.initializeservice(abstractserviceregistryimpl.java:234) @ org.hibernate.service.internal.abstractserviceregistryimpl.getservice(abstractserviceregistryimpl.java:206) @ org.hibernate.cfg.configuration.buildtyperegistrations(configuration.java:1885) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1843) @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$4.perform(entitymanagerfactorybuilderimpl.java:850) @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$4.perform(entitymanagerfactorybuilderimpl.java:843) @ org.hibernate.boot.registry.classloading.internal.classloaderserviceimpl.withtccl(classloaderserviceimpl.java:397) @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl.build(entitymanagerfactorybuilderimpl.java:842) @ org.hibernate.jpa.hibernatepersistenceprovider.createentitymanagerfactory(hibernatepersistenceprovider.java:75) ... 20 more caused by: javax.naming.namenotfoundexception: name [java:comp/env/jdbc/distorehvt] not bound in context. unable find [java:comp]. @ org.apache.naming.namingcontext.lookup(namingcontext.java:819) @ org.apache.naming.namingcontext.lookup(namingcontext.java:153) @ javax.naming.initialcontext.lookup(initialcontext.java:415) @ org.hibernate.engine.jndi.internal.jndiserviceimpl.locate(jndiserviceimpl.java:114) ... 36 more
i want solution using jndi (i not want save passwords in code) , persistence.xml, jpa standard. there come more persistence units second one. use spring 3.2.3-final.
Comments
Post a Comment