-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate in DAO pattern
PostPosted: Sun Jan 24, 2010 6:00 pm 
Newbie

Joined: Wed Jan 06, 2010 9:09 pm
Posts: 14
Hi All,

I'm using hibernate and have started writing my DAO classes. Is it allowed / a good idea to have a DAO class do some calculations and other lookups in order to generate the top level data needed by the caller, or should Hibernate DAOs just be wrappers around basic CRUD functionality only?

TY


Top
 Profile  
 
 Post subject: Re: Hibernate in DAO pattern
PostPosted: Sun Jan 24, 2010 11:27 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Typically, calculations should go in a service layer. So, client does its stuff, calls a service layer, and the service layer calls the DAOs. So, my first desire is to use a service layer.

For a good tutorial on using DAOs with Hibernate, check out this tutorial:

http://www.hiberbook.com/HiberBookWeb/l ... vanceddaos

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Hibernate in DAO pattern
PostPosted: Mon Jan 25, 2010 5:53 am 
Newbie

Joined: Mon Jan 25, 2010 3:25 am
Posts: 6
Hi,

The business logic (some calculations and other lookups in order to generate the top level data) should be handled at the business service layer.
DAOs should be wrappers around CRUD functionalities.

Regards,
Sudhir


Top
 Profile  
 
 Post subject: Re: Hibernate in DAO pattern
PostPosted: Mon Jan 25, 2010 3:22 pm 
Newbie

Joined: Wed Jan 06, 2010 9:09 pm
Posts: 14
RE the examples shown in the
http://www.hiberbook.com/HiberBookWeb/l ... vanceddaos
link, what's the reason that the DAO class contains begin and commit transaction methods?
Shouldn't the starting, rollback and finishing of transactions be a detail implemented within the DAO itself?

TY,
Fred


Top
 Profile  
 
 Post subject: Re: Hibernate in DAO pattern
PostPosted: Tue Jan 26, 2010 2:29 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Simplicity is the only reason those methods are there, and even then, I would prefer them to be outside of the DAO.

No, no, no! You don't want transaction handling inside of the DAO! TOTAL ANTI-PATTERN!

The layer invoking the DAO should be starting and committing the transactions - NEVER inside of the DAO. You may want to hit three tables and do it all inside of one transaction. If each DAO starts and commits its own transactions, you can't call three different DAOs and have them run as a single unit of work. So, the transaction stuff should be outside of the DAOs.

Again, the DAO example there is later used in a simple JSF application. A service layer begins and commits transactions, and the DAO simply had little methods to expose the basic transaction methods. Even then, I dislike the DAO being the source of the transaction; a different abstraction would be even better.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.