title: Can't create EntityManager using JTA Datasource
body: <p>I'm trying to instantiate an EntityManager using a JTA Datasource, but I always get NullPointerException.
  Here's my environment: 
  Server : JBossAS 7.0
  JPA : 2.0
  DB : MySQL</p>
  
  <p>persistence.xml</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="WebStock" transaction-type="JTA"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;jta-data-source&gt;stockmanagementDS&lt;/jta-data-source&gt;
          &lt;class&gt;prv.stockmanagement.business.Product&lt;/class&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>On a dao class I'm trying this :</p>
  
  <pre><code>@PersistenceContext(type=PersistenceContextType.EXTENDED, unitName="WebStock")
  private EntityManager em;
  </code></pre>
  
  <p>but when I use its getter, I get NullPointerException here :  </p>
  
  <pre><code>public EntityManager getEntityManager() {
          return em;
      }
  </code></pre>
  
  <p>the debug shows it's null.</p>
  
  <p>Here's the datasource definition in the standalone.xml :</p>
  
  <pre><code>&lt;datasource jndi-name="stockmanagementDS" pool-name="stockmanagement" enabled="true" jta="true" use-java-context="true" use-ccm="true"&gt;
                      &lt;connection-url&gt;
                          jdbc:mysql://localhost:3306/kitchen_stock
                      &lt;/connection-url&gt;
                      &lt;driver&gt;
                          mysql
                      &lt;/driver&gt;
                      &lt;security&gt;
                          &lt;user-name&gt;
                              root
                          &lt;/user-name&gt;
                          &lt;password&gt;
                              rootroot
                          &lt;/password&gt;
                      &lt;/security&gt;
                      &lt;statement&gt;
                          &lt;prepared-statement-cache-size&gt;
                              100
                          &lt;/prepared-statement-cache-size&gt;
                          &lt;share-prepared-statements/&gt;
                      &lt;/statement&gt;
                  &lt;/datasource&gt;
  </code></pre>
  
  <p>"mysql" is the name of the driver I defined after the datasource : </p>
  
  <p><code>&lt;drivers&gt;
       &lt;driver name="mysql" module="com.sql.mysql"&gt;
              &lt;driver-class&gt;
                   com.mysql.jdbc.Driver
              &lt;/driver-class&gt;
       &lt;/driver&gt;
       &lt;driver name="h2" module="com.h2database.h2"&gt;
              &lt;xa-datasource-class&gt;
                    org.h2.jdbcx.JdbcDataSource
              &lt;/xa-datasource-class&gt;
       &lt;/driver&gt;
  &lt;/drivers&gt;</code></p>
  
  <p>Persistence is under META-INF, it's dynamic web project, and it's working well. The classing using the entity manager is not an EJB.</p>
  
  <p>Any hint?</p>