@Inject only works in page, component and mixin classes 
(as stated on the mailinglist some hours ago :))

Services can be injected into other services by declaring them in the
constructor of the service:

your dao constructor:
...
public DAO (EntityManager em){...}

your module:

public static void bind(ServiceBinder binder) {
    binder.bind(DAOInterface.class,DAOImpl.class)
}


or you can have your own build method

buildDAO(EntityManager em) {
   return new DAOImpl(em)
}

or even:

buildDAO(EntityManager em) {
   DAO d = new DAOImpl();
   d.setEntityManager(em);
   return d;
}

This works only if the EntityManager itself is declared as a Tapestry IOC 
service

i'd also recommend you to look into the source of the Tapestry-Hibernate 
project
to see how it works. see Tapestry-IOC documentation for more details, 
especially 
the tapestry-ioc cookbook

g,
kris




Leon Derks <[EMAIL PROTECTED]> 
28.04.2008 14:40
Bitte antworten an
"Tapestry users" <users@tapestry.apache.org>


An
"users@tapestry.apache.org >> Tapestry users" <users@tapestry.apache.org>
Kopie

Thema
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 [EMAIL PROTECTED] 
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);
   }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to