I've been refreshing my skills with Hibernate and Spring lately, and one of the activities I took upon myself was to update the SpringBlog application provided in the book Pro Spring from Apress.
Since the book was written in 2005, it is based on previous releases of Spring (v1.1) and Hibernate (v2.1). The current release of Spring is v2.02, and the current version of Hibernate is v3.2. (as of Jan 2007)
Here is a list of some of some upgrades I made to take advantage of improvements in Spring v2 and Hibernate v3 features:
- Changed from declarative transaction to annotation-based transactional support
- Modified the HibernateDAO classes so they do not rely on HibernateDaoSupport. The SessionFactory is injected via Spring.
I also had to make several changes due to runtime problems with the newer libraries. Maybe these worked in Spring v1.1 and Hibernate 2, they didn't work for me with Spring v2 and Hibernate v3 without modification:
- HibernateCommentDao.save() - I had to create a new Comment class and copy the data from the incoming comment variable (which is really based on CommentForm). This is the exact same things that existing code in the HibernateEntryDao.save() class had to do with EntryForm.
- Hibernate*Dao.delete() -- The delete() feature did not work for any of the Domain classes. There were errors because you cannot create objects with null values in non-null properties. And therefore - you couldn't delete those objects. So the technique used in the downloaded app doesn't work. Instead I had to perform a load() followed by a delete(). The book Java Persistence with Hibernate describes this type of logic in Section 9.3.1 (page#407).
- I had to add a Filter based on the Spring class OpenSessionInViewFilter in my web.xml. The reasons it were necessary are described in this link from the Hibernate site.
- Updated all package references to use the newer hibernate3 package provided in the spring-hibernate3.jar
It took me several hours to get this working -- I hope these suggestions save others some time!