Thursday, January 18, 2007

Updating the SpringBlog App from "Pro Spring" to Spring v2 and Hibernate v3.2



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!

2 comments:

Rowan said...

Hi,
Im currently readin Pro Spring, and only jsut realised that the delete doesnt work.. I knew some1 else somewhere has done this :o) when I read ur blog I got excited.
Would it be possible to share ur changes? maybe we can contibute to each others work..
Rowan
r9star@hotmail.com

Francis Wong said...

Hi Rowan - I'm sorry, but that post was from a year-and-a-half ago, and I don't have the code samples any longer. I did however describe the changes I had to make to get the sample working in the blog post itself. Hopefully this is enough to point you in the right direction. Good Luck!