title: EntityManager injected by Spring but null on first use
body: <p>I am just working on a basic webapp that implements spring + hibernate entitymanager</p>
  
  <p>I set up my dao and context exactly as described by this post: 
  <a href="http://blog.springsource.com/2006/08/07/using-jpa-in-spring-without-referencing-spring/" rel="nofollow">http://blog.springsource.com/2006/08/07/using-jpa-in-spring-without-referencing-spring/</a></p>
  
  <p>And created a test for it as shown here:
  <a href="http://lstierneyltd.com/blog/development/examples/unit-testing-spring-apps-with-runwithspringjunit4classrunner-class/" rel="nofollow">http://lstierneyltd.com/blog/development/examples/unit-testing-spring-apps-with-runwithspringjunit4classrunner-class/</a></p>
  
  <p>For some reason when I try to access the entitymanager for creating a query, its null. </p>
  
  <p>However from setting a breakpoint inside the setter method for the entitymanager I can see that Spring is injecting it correctly, and the field is being initialized.  </p>
  
  <p>Any clue as to why the entitymanager might be getting nullified after the setter has returned ?</p>
  
  <p>Edit: 
  The dao code where I am setting the breakpoint:</p>
  
  <pre><code>public class ProductDaoImpl implements ProductDao {
  
  private EntityManager entityManager;
  
  @PersistenceContext
  public void setEntityManager(EntityManager entityManager) {
      this. entityManager = entityManager;
  }
  
  public Collection loadProductsByCategory(String category) {
      return entityManager.createQuery("from Product p where p.category = :category")
          .setParameter("category", category).getResultList();
  }
  }
  </code></pre>