aspose file tools*
The moose likes Other Application Frameworks and the fly likes Spring DAO + Hibernate; is this right? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring DAO + Hibernate; is this right?" Watch "Spring DAO + Hibernate; is this right?" New topic
Author

Spring DAO + Hibernate; is this right?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, I have successful integrated Spring's DAO into my web application that uses hibernate. By successfully I mean, it appears to be working. However, I would just like some feedback on whether or not the way I am doing it is correct/best, etc. So forgive the long post, but here is all the relavent code. I am NOT including any hibernate mapping files because this is not a hibernate question. This is specifically about how I integrated Spring's DAO support. Thanks.

applicationContext.xml


UserService


UserServiceImpl


UserDAO


UserDaoHib


User Login Snippet


Ok, so how does all that look? What would you do differently?


GenRocket - Experts at Building Test Data
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Looks alright. You could get rid of the setHibernateTemplate() method, though, by having your DAO implementation class extend HibernateDaoSupport.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Lasse Koskela:
Looks alright. You could get rid of the setHibernateTemplate() method, though, by having your DAO implementation class extend HibernateDaoSupport.


Yeah, I looked at that before. The problem I was having was because of the HibernateCallback() and I was getting on NullPointerException when accessing hibernateTemplate on this line:

return (User)hibernateTemplate.execute(new HibernateCallback() {

I may not need to use the Callback though so I will look at that. Thanks.
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Gregg,

Glad to see you broke down and are giving Spring a try .
Watch out though, using it is very addictive.

HibernateDaoSuport just makes working with HibernateTemplate a little easier and you can skip it and go directly with HibernaeTemplate if you need to extend your DAo from some other class. HibernateTemplate itself has lots of convenience methods (find, load, saveOrUpdate, delete) that you can use instead of execute. For more complicated operations, you can use execute but then you will have to implement the HibernateCallback and manipulate the Hibernate Session it provides you.

Here's a snippet from one of my apps:



As you can see, most of the Dao is simple one-liner methods, thanks to Spring's HibernateTemplate's handling of Session management/plumbing and its wrapping of Hibernate's checked exceptions.
[ March 03, 2005: Message edited by: Ken Krebs ]

kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks Ken. I have another class I have converted for looking up Employees and I am using the HibernateTemplate methods. The only reason I didn't initially for the user was because I knew that in findUserById I was returning a unique result. At the time, I didn't realize all that did was:

return (User)list.get(0)

Now that I know that's all that method really did, I'll be changing it and losing the Callback().

Thanks again.
_neeraj_kumar_
Greenhorn

Joined: Feb 15, 2005
Posts: 4
Have a look at the petclinic example that comes along with Spring. Petclinic absolutely rocks and can teach one a thing or two about not just Spring but also good design patterns. Petclinic also shows how to write junit tests for your DAOs. How to write DAOs in such a way that you could switch from Hibernate to iBatis with minimal changes.


Neeraj Kumar<br /><a href="http://weblog.neeraj.name" target="_blank" rel="nofollow">http://weblog.neeraj.name</a>
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Gregg Bolinger:
I was getting on NullPointerException when accessing hibernateTemplate on this line:

return (User)hibernateTemplate.execute(new HibernateCallback() {
This is due to the fact that hibernateTemplate is lazily-instantiated. You can call getHibernateTemplate() instead, and it will create it for you.

I also recommend ditching the callback pattern if you can. It will make the code easier to read.

Good job so far! You're picking this up pretty darn quick.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by David Harkness:
This is due to the fact that hibernateTemplate is lazily-instantiated. You can call getHibernateTemplate() instead, and it will create it for you.

I also recommend ditching the callback pattern if you can. It will make the code easier to read.

Good job so far! You're picking this up pretty darn quick.


Thanks David! The only reason I was using the call back pattern was because I hate HQL and I really like using the Criteria and Expressions API from Hibernate. I don't believe there is a way to do this without hooking into hibernate with the callback pattern. If I am wrong, please let me know.
 
wood burning stoves
 
subject: Spring DAO + Hibernate; is this right?