Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring DAO + Hibernate; is this right?

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks alright. You could get rid of the setHibernateTemplate() method, though, by having your DAO implementation class extend HibernateDaoSupport.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic