title: Configuring Multiple Persistence Units In JPA In Same XML File
body: <p>I have two databases A,B.I have a table called asample in database A and bsample in database B. Now I want to transfer the data from bsample table of B database to asample table of A database. For this I have to use two persistence units in my persistence.xml file. I am using JPA, Spring. I am new to jpa. Can anyone tell me how can I achieve this.</p>
  
  <p>If I use the below mentioned code I am getting this exception:</p>
  
  <pre><code>ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in ServletContext resource [/WEB-INF/classes/META-INF/spring-context/dao-DB1.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediationEntityManagerFactory' defined in ServletContext resource [/WEB-INF/classes/META-INF/spring-context/dao-DB2.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: DB2] Unable to build EntityManagerFactory
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:710)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:410)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
      at javax.servlet.GenericServlet.init(GenericServlet.java:160)
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
      at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
      at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediationEntityManagerFactory' defined in ServletContext resource [/WEB-INF/classes/META-INF/spring-context/dao-DB2.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: DB2] Unable to build EntityManagerFactory
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:398)
      at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:275)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.&lt;init&gt;(PersistenceExceptionTranslationInterceptor.java:79)
      at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.&lt;init&gt;(PersistenceExceptionTranslationAdvisor.java:70)
      at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:99)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1439)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1408)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      ... 26 more
  Caused by: javax.persistence.PersistenceException: [PersistenceUnit: DB2] Unable to build EntityManagerFactory
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:900)
      at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
      ... 41 more
  </code></pre>
  
  <p>Iplaced the two persistence units in my persistence.xml file and I have two dao files as dao-DB1.xml, dao-DB2.xml there I am configuring the EntityManager as mentioned in the answer.</p>