Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA and DAO pattern

 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Will Myers
Ranch Hand
Posts: 375
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't you just use the @Resource annotation and have the container inject it? or do a JNDI lookup?
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, lookup could be an option.
 
Kumar Amit
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pratik Bhavsar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will transaction propagation occur if using unmanaged POJO as DAO?
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if the transaction has been started by the method calling the DAO.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic