• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA and DAO pattern

 
Ranch Hand
Posts: 133
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, lookup could be an option.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will transaction propagation occur if using unmanaged POJO as DAO?
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if the transaction has been started by the method calling the DAO.
 
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic