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.
User Login Snippet
Ok, so how does all that look? What would you do differently?
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
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:
Now that I know that's all that method really did, I'll be changing it and losing the Callback().
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.
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com