title: How to have more than one persistence units in an JPA 2 application?
body: <p><strong>My system:</strong></p>
  
  <p>Eclipse Oxygen/JPA 2/JSF 2.2/Hibernate 4/JBoss AS 7</p>
  
  <p><strong>My condition:</strong></p>
  
  <p>I have an application with two persistence units (PU) declared in <code>persistence.xml</code>.</p>
  
  <p><strong>My JBoss error:</strong></p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment "test.war".  Either change the application 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.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:69)
      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getPersistenceUnit(JPAAnnotationParseProcessor.java:284)
      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getBindingSource(JPAAnnotationParseProcessor.java:220)
      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processField(JPAAnnotationParseProcessor.java:151)
      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processPersistenceAnnotations(JPAAnnotationParseProcessor.java:118)
      at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.deploy(JPAAnnotationParseProcessor.java:90)
      at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.0.Final.jar:7.1.0.Final]
      ... 5 more
  </code></pre>
  
  <p><strong>My problem:</strong></p>
  
  <p>I use a framework that hides all the details on the EntityManager lifecycle. This framework gives me an ancestor class and I build all my code in a subclass, not caring about managing the EntityManager.</p>
  
  <p>This ancestor class <strong>does not</strong> inject ou annotate the EntityManager, it is created by code when needed for the first time, but the exception above is thrown by JBoss during the application start when I have more than one PU.</p>
  
  <p>I wrote a code in the ancestor to accept the @PersistenceUnit annotation in my subclass and to use the name set in the annotation when creating the EntityManagerFactory. When no annotation is used, the code finds out the first PU name and used it. So, the first PU existing in <code>persistence.xml</code> is understood as a default PU name.</p>
  
  <p>However, even not injecting any EntityManagers, I still have the above exception.</p>
  
  <p>What is missing in my solution?</p>