aspose file tools*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes JPA and DAO pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "JPA and DAO pattern" Watch "JPA and DAO pattern" New topic
Author

JPA and DAO pattern

Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

Hi All,

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?

Regards,
Krzysztof Koziol


SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCEA/OCMJEA 5.0
Will Myers
Ranch Hand

Joined: Aug 05, 2009
Posts: 331

can't you just use the @Resource annotation and have the container inject it? or do a JNDI lookup?
Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

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.
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

You could model DAO as stateless bean. Then you could inject the entity manger. There has been discussion on pro/cons in this thread.

DAO as session beans

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.
Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

Right, lookup could be an option.
Kumar Amit
Ranch Hand

Joined: Aug 13, 2001
Posts: 103
Rishi Shehrawat wrote:You could model DAO as stateless bean. Then you could inject the entity manger. There has been discussion on pro/cons in this thread.

DAO as session beans

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.


SCJP, SCJD, SCEA
Pratik Bhavsar
Greenhorn

Joined: Feb 04, 2009
Posts: 20
Will transaction propagation occur if using unmanaged POJO as DAO?
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

Yes, if the transaction has been started by the method calling the DAO.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA and DAO pattern