This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm going to use JPA and DAO pattern in my design. As we know persistence context (EntityManager) could be injected in the Session Bean since it's managed by app server but could not be injected in DAO itself. The question is how could we access entity manager in the DAO classes? It could be passed as an argument in the method call but I'm looking for more elegant way to do this. Any ideas?
will myers wrote:can't you just use the @Resource annotation and have the container inject it?
I dont't think so. DAO is just a POJO without any specific annotation or declaration in an application descriptor therefore is not managed by the container like Session Beans, Managed Beans, Servlets etc.
You could just lookup the entity manger in your DAO. In case you are calling all DAO's from session beans the container will manage the lifecycle of entity manager.
I reckon using another stateless session bean for just having a DAO pattern is an overkill. But again its my personal opinion. I would implement the CRUD operations (being invoked by the presentation tier) in the session facade itself and will use SLSB DAO only if specialised business logic is required.