title: Multiple persistence unit in persistence.xml file with JPA 2.1
body: <p>I am trying to deploy an application that connects to Oracle Database and MySQL. I am using JPA 2.1, Hibernate 4.3.7, Spring, Spring Data and WildFly 8.2 but I am getting some errors when deploying the application. Eclipse shows error. He says that I cannot use more than one persistence unit. I was confused because I found some articles on the web showing files with more than one persistence unit. <a href="http://www.mastertheboss.com/jboss-frameworks/hibernate-jpa/jpa-configuration/how-to-use-multiple-database-in-persistencexml" rel="nofollow">Like this one</a>.</p>
  
  <p>First of all my <code>persistence.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;persistence-unit name="authwsPU" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:/datasource/authwsds&lt;/jta-data-source&gt;
  
          ..My MySQL mapping classes...
  
          &lt;properties&gt;
              &lt;!-- MySQL --&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;
  
              &lt;property name="hibernate.show_sql" value="true"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
              &lt;property name="hibernate.id.new_generator_mappings" value="true"/&gt;
              &lt;property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/authwsEMF" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  
      &lt;persistence-unit name="antaresPU" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:/datasource/antaresds&lt;/jta-data-source&gt;
  
          ...My Oracle mapping classes...
  
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/&gt;
              &lt;property name="hibernate.show_sql" value="true"/&gt;
              &lt;property name="hibernate.id.new_generator_mappings" value="true"/&gt;
              &lt;property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/antaresEMF" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I made change on <code>jboss-web.xml</code> too:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee
                                 http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd"&gt;
  
      &lt;context-root&gt;/authws&lt;/context-root&gt;
  
      &lt;persistence-context-ref id="autenticacaoCtx"&gt;
          &lt;persistence-context-ref-name&gt;auth_ws/authEMF&lt;/persistence-context-ref-name&gt;
          &lt;persistence-unit-name&gt;authwsPU&lt;/persistence-unit-name&gt;
      &lt;/persistence-context-ref&gt;
  
      &lt;persistence-context-ref id="antaresCtx"&gt;
          &lt;persistence-context-ref-name&gt;auth_ws/antaresEMF&lt;/persistence-context-ref-name&gt;
          &lt;persistence-unit-name&gt;antaresPU&lt;/persistence-unit-name&gt;
      &lt;/persistence-context-ref&gt;
  &lt;/jboss-web&gt;
  </code></pre>
  
  <p>And the errors. Eclipse, warned me, but I ignored.:</p>
  
  <pre><code>Multiple persistence units defined - only the first persistence unit will be recognized
  </code></pre>
  
  <p>Then I deployed and WildFly shows this error:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "authws.war".  Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
      at org.jboss.as.jpa.container.PersistenceUnitSearch.ambiguousPUError(PersistenceUnitSearch.java:187)
      at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinDeployment(PersistenceUnitSearch.java:153)
      at org.jboss.as.jpa.container.PersistenceUnitSearch.findPersistenceUnitSupplier(PersistenceUnitSearch.java:75)
      at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:64)
      at org.jboss.as.jpa.processor.JPAAnnotationProcessor.getPersistenceUnit(JPAAnnotationProcessor.java:372)
      at org.jboss.as.jpa.processor.JPAAnnotationProcessor.getBindingSource(JPAAnnotationProcessor.java:296)
      at org.jboss.as.jpa.processor.JPAAnnotationProcessor.processMethod(JPAAnnotationProcessor.java:206)
      at org.jboss.as.jpa.processor.JPAAnnotationProcessor.processPersistenceAnnotations(JPAAnnotationProcessor.java:143)
      at org.jboss.as.jpa.processor.JPAAnnotationProcessor.deploy(JPAAnnotationProcessor.java:100)
      at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:159) [wildfly-server-8.2.0.Final.jar:8.2.0.Final]
      ... 5 more
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am updating the question with more details. First, the file where I create the EntityManagersFactory for Spring Data:</p>
  
  <pre><code>&lt;jee:jndi-lookup jndi-name="java:jboss/poEMF" id="poEntityManagerFactory" expected-type="javax.persistence.EntityManagerFactory" /&gt;
  &lt;jpa:repositories base-package="br.com.po.dao" entity-manager-factory-ref="poEntityManagerFactory"  /&gt;
  
  &lt;jee:jndi-lookup jndi-name="java:jboss/antaresEMF" id="antaresEntityManagerFactory" expected-type="javax.persistence.EntityManagerFactory" /&gt;
  &lt;jpa:repositories base-package="br.com.antares.dao" entity-manager-factory-ref="antaresEntityManagerFactory" /&gt;
  </code></pre>
  
  <p>Reading more about this online, I saw that the problem can be specification of the annotation <code>@PersistenceUnit</code> where I have to specify the <code>unitName</code> like <code>@PersistenceUnit(unitName="defaultPersistenceUnit")</code>. <strong>But, how I do this if I am using Spring Data?</strong></p>