• 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

Spring DAO + Hibernate; is this right?

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic