title: JPA Entity Manager Factory injection fails (JPA 2, Eclipselink, J2EE 6)
body: <p>I am trying to get a small sample web app up and running but I have run into a problem injecting the Entity Manager Factory.</p>
  
  <p>My persistence.xml is as follows;</p>
  
  <pre><code>&lt;persistence version="2.0" xmlns=" http://java.sun.com/xml/ns/persistene"
           xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/persistence ttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  &lt;persistence-unit name="main" transaction-type="JTA"&gt;
      &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/Maindb&lt;/jta-data-source&gt;
      &lt;properties&gt;
          &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables" /&gt; 
          &lt;property name="eclipselink.ddl-generation.output-mode" value="database" /&gt; 
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>The web application has two functions; return a customer and a list of items.</p>
  
  <p>The CustomerResource object injects the entity manager factory as follows:</p>
  
  <pre><code>@PersistenceUnit(unitName="main")
  private EntityManagerFactory emf;
  </code></pre>
  
  <p>and queries the persistence layer by the following code;</p>
  
  <pre><code>EntityManager em = emf.createEntityManager();
  Customer customer = (Customer) em.find(Customer.class, customerID);
  </code></pre>
  
  <p>This works with no problems (that I am aware of), I get the expected data returned.</p>
  
  <p>The ItemResource object does the same thing against the same persistence unit.</p>
  
  <pre><code>@PersistenceUnit(unitName="main")
  private EntityManagerFactory emf;
  </code></pre>
  
  <p>But the injection fails and emf is always null.</p>
  
  <pre><code>EntityManager em = emf.createEntityManager(); &lt;- emf is null here
  </code></pre>
  
  <p>I am unsure of what I have done wrong here, my guess is that I am using the entity manager factory incorrectly.</p>
  
  <p>Any help would be much appreciated! Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>I was taking out the troublesome code to put in a war file for everyone to look at which helped me isolate the problem.</p>
  
  <p>The issue seems to be with the url patterns I am using.</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5"&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;com.sun.xml.ws.transport.http.servlet.WSServletContextListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Item&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.xml.ws.transport.http.servlet.WSServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Item&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Item/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Customer&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.xml.ws.transport.http.servlet.WSServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Customer&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Customer&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>sun-jaxws.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"&gt;
      &lt;endpoint name="Item" implementation="com.test.item.ItemResource" url-pattern="/Item/*" /&gt;
      &lt;endpoint name="Customer" implementation="com.test.customer.CustomerResource" url-pattern="/Customer" /&gt;
  &lt;/endpoints&gt;
  </code></pre>
  
  <p>The item resource right now has two web methods;</p>
  
  <ol>
  <li><p>Get Item Details <br>
  Type: Get <br>
  Path: / <br>
  Web param: item id<br></p></li>
  <li><p>Get Item List<br>
  Type: Get<br>
  Path: /list<br>
  Web param: Item Colour<br></p></li>
  </ol>
  
  <p>With the wild cards in the url patterns the entity manager is always null. If I remove the wild cards then I can successfully request an item, put I cannot request a list of items because it is not mapped.</p>
  
  <p>The customer resource requests are always successful because it does not contain any wild cards in the mappings.</p>
  
  <p>Thanks</p>