Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] T5: JPA EntityManager + Tapestry Injection

I have added an JPA entity manager to my AppModule, in the same way this is done for an HibernateSessionManager/Session in the tapestry-hibernate project.
See the code below for some details.

I inject this entity manager with the @Inject into my DAO object.
@Inject
private EntityManager entityManager

In my DAO method I use the injected entityManager to build queries like this: entityManager.createNamedQuery("findProductsByCategory").setParameter("productCategory", ProductCategory.Lamps).getResultList();

But the problem is that the entityManager is null. It seems that the entityManager is not correctly injected into my DAO class. Because when I use the entityManager directly in my Java Page class and execute the same code, everything works well.

Java Page code:
public void onActivate() {
   // this results in an null pointer exception.
   products = productDAO.findAllByCategory(ProductCategory.Lamps);

   //this is working.
products = entityManager.createNamedQuery("findProductsByCategory").setParameter("productCategory", ProductCategory.Lamps).getResultList();
}

What do I need to do so I can use my DAO classes?

Leon


AppModule.java:
   /**
* The JPAEnitityManagerService manages the entity manager on a per-thread/per-request basis. A {@link javax.persistence.EntityTransaction} is created
    * initially, and is committed at the end of the request.
    */
   @Scope(PERTHREAD_SCOPE)
public static JPAEntityManagerService build(PerthreadManager perthreadManager) { JPAEntityManagerServiceImpl jpaService = new JPAEntityManagerServiceImpl();
       perthreadManager.addThreadCleanupListener(jpaService);
       return jpaService;

   }

public static EntityManager build(JPAEntityManagerService jpaService, PropertyShadowBuilder propertyShadowBuilder) { return propertyShadowBuilder.build(jpaService, "entityManager", EntityManager.class);
   }


Back to the top