title: Using JPA2 in Tomcat 6: @PersitenceContext doesn't work, EntityManager is null
body: <p>I am using JSF2 with Pure JPA2.
  But the problem is with entityManager,  </p>
  
  <pre><code>@PersistenceContext
  private EntityManager entityManager;
  </code></pre>
  
  <p>Here entityManager is not getting injected and always null.
  Can some one please help me what is wrong in my code.</p>
  
  <p>Here is my configuration.</p>
  
  <p>User.java</p>
  
  <pre><code>    @Entity
      @Table(name="USER")
      public class User {
  
          private int id;
          private String name;
          private String surname;
  
          @Id
          @SequenceGenerator(name="user_id_seq_gen", sequenceName="USER_ID_GEN_SEQ", allocationSize=1)
          @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="user_id_seq_gen")
          @Column(name="ID", unique = true, nullable = false)
          public int getId() {
              return id;
          }
  
          public void setId(int id) {
              this.id = id;
          }
  
          @Column(name="NAME", unique = true, nullable = false)
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
  
          @Column(name="SURNAME", unique = true, nullable = false)
          public String getSurname() {
              return surname;
          }
  
          public void setSurname(String surname) {
              this.surname = surname;
          }   
  
      }
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;persistence 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" version="2.0"&gt;
             &lt;persistence-unit name="testUnit" transaction-type="RESOURCE_LOCAL"&gt;
                &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
                &lt;non-jta-data-source&gt;java:/comp/env/testDS&lt;/non-jta-data-source&gt;
                &lt;class&gt;com.user.external.test.User&lt;/class&gt;
                &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
                &lt;properties&gt;
                   &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/&gt;
                   &lt;property name="hibernate.max_fetch_depth" value="6"/&gt;
                   &lt;property name="hibernate.show_sql" value="false"/&gt;
                   &lt;property name="hibernate.format_sql" value="true"/&gt;
                &lt;/properties&gt;
             &lt;/persistence-unit&gt;
          &lt;/persistence&gt;
  </code></pre>
  
  <p>Tomcat Server.xml</p>
  
  <pre><code>    &lt;Context docBase="usertools" path="/usertools" reloadable="true"&gt;
          &lt;ResourceLink name="testDS" 
                    global="testDS"  
                    type="javax.sql.DataSource"/&gt;
      &lt;/Context&gt; 
  </code></pre>
  
  <p>TestBean.java</p>
  
  <pre><code>    @ManagedBean(name="testBean")
      @ViewScoped
      public class TestBean implements Serializable{
  
          @ManagedProperty("#{testService}")
          private ITestService testService;
  
          public void saveData(User user){
              testService.saveUser(user);
          }
  
          public void setTestService(ITestService testService){
              this.testService = testService;
          }
  
          public ITestService getTestService(){
              return testService;
          }
      }
  </code></pre>
  
  <p>TestServiceImpl.java</p>
  
  <pre><code>    @ManagedBean(name="testService")
      @ApplicationScoped
      public class TestServiceImpl implements ITestService {
  
          @ManagedProperty(value="#{testDAO}")
          private ITestDAO testDAO;
  
          public void saveUser(User user){
              testDAO.saveUser(user);
          }
  
          public void setTestDAO(ITestDAO testDAO){
              this.testDAO = testDAO;
          }
  
          public ITestDAO getTestDAO(){
              return testDAO;
          }
  
      }   
  </code></pre>
  
  <p>TestDao.java</p>
  
  <pre><code>    @ManagedBean(name = "testDAO")
      @ApplicationScoped
      public class TestDAO implements ITestDAO {
  
          @PersistenceContext
          private EntityManager entityManager; // is null
  
          public void saveUser(User user){
              entityManager.persist(user); // Getting Null Pointer Here....
          }   
  
          public void setEntityManager(EntityManager entityManager) {
              this.entityManager = entityManager;
          }
  
          public EntityManager getEntityManager() {
              return entityManager;
          }
  
      }
  </code></pre>
  
  <p>I have tried with @PersistenceContext name &amp; unitName attributes. But still it is not working. Also tried with resource config in web.xml   </p>
  
  <pre><code>    &lt;persistence-context-ref&gt;
              &lt;persistence-context-ref-name&gt;testUnit&lt;/persistence-context-ref-name&gt;
              &lt;persistence-unit-name&gt;testUnit&lt;/persistence-unit-name&gt;
          &lt;/persistence-context-ref&gt;
  
          &lt;resource-ref&gt;
              &lt;description&gt;DB Connection&lt;/description&gt;
              &lt;res-ref-name&gt;testUnit&lt;/res-ref-name&gt;
              &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
              &lt;res-auth&gt;Container&lt;/res-auth&gt;
          &lt;/resource-ref&gt;
  </code></pre>
  
  <p>Still No Luck.
  Can some one please help me.</p>