title: Fixing "Could not resolve a persistence unit..." errors when PU is specified, found
body: <p>I'm running Glassfish 3.1-SNAPSHOT as of today (2010-11-12).</p>
  
  <p>I'm using the embedded EJBContainer.</p>
  
  <p>On the classpath, as reported by the EJBContainer, I have a META-INF/persistence.xml.  This file defines two persistence units: one called "ngp" and one called "cx".</p>
  
  <p>Debugging output shows that the Glassfish JPA deployer finds it, and recognizes both the cx PU and the ngp PU.</p>
  
  <p>The EJBContainer bombs out with the following all-too-common JPA error:</p>
  
  <pre><code>java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [cx] in the scope of the module called [/Users/ljnelson/Projects/foo/target/test-classes/]. Please verify your application.
  at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:693)
  at com.sun.enterprise.deployment.EjbBundleDescriptor.findReferencedPUs(EjbBundleDescriptor.java:910)
  at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:140)
  at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:869)
  at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:410)
  at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
  at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:193)
  at org.glassfish.kernel.embedded.EmbeddedDeployerImpl.deploy(EmbeddedDeployerImpl.java:142)
  at org.glassfish.ejb.embedded.EJBContainerImpl.deploy(EJBContainerImpl.java:135)
  at org.glassfish.ejb.embedded.EJBContainerProviderImpl.createEJBContainer(EJBContainerProviderImpl.java:132)
  at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:127)
  </code></pre>
  
  <p>I stress again that the deployment logs show that at least the deployer encounters both persistence units.</p>
  
  <p>The class that wants to use the "cx" PU contains the usual boilerplate:</p>
  
  <pre><code>@PersistenceContext(unitName="cx")
  private EntityManager em;
  </code></pre>
  
  <p>The persistence.xml is present in (the usual Maven place) <code>target/test-classes/META-INF</code> and looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" 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"&gt;
  
      &lt;persistence-unit name="cx" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:global/jdbc/H2Test&lt;/jta-data-source&gt;
          &lt;!-- snip --&gt;
      &lt;/persistence-unit&gt;
  
      &lt;persistence-unit name="ngp" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:global/jdbc/H2Test&lt;/jta-data-source&gt;
          &lt;!-- snip --&gt;
      &lt;/persistence-unit&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p>The Glassfish embedded EJBContainer, while doing its work, outputs this:</p>
  
  <pre><code>Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINE: Got com.sun.enterprise.deployment.node.PersistenceUnitNode@2026c088
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINER: With attribute name
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINER: With value cx
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINE: in class com.sun.enterprise.deployment.PersistenceUnitDescriptor  method  setName with  cx
  ...snip...
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINE: Got com.sun.enterprise.deployment.node.PersistenceUnitNode@1648ff68
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINER: With attribute name
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINER: With value ngp
  Nov 12, 2010 1:24:05 PM com.sun.logging.LogDomains$1 log
  FINE: in class com.sun.enterprise.deployment.PersistenceUnitDescriptor  method  setName with  ngp
  </code></pre>
  
  <p>Troubleshooting recipes, anyone? </p>