Spring, Hibernate and LazyInitializationExceptions

I’ve recently started using Spring for its comprehensive support for Hibernate 3. It makes writing DAOs a snap. There are a couple of wrinkles, however, one is the fact that lazy loading in Hibernate 3 is the default, combined with the fact that Spring will close the Hibernate Session automatically, giving you a LazyInitializationException when you try to access mapped fields from your persistent objects. The solution to this is the “Open Session In View” pattern, which Spring provide a ready-made filter for. Shown below is an extract from my PersistentTestCase, superclass, which all my DAO tests extend, and voila, no more Lazy Exceptions.

protected void setUp() throws ClassNotFoundException, SQLException {
      ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("application_context.xml");
      factory = (BeanFactoryappContext;
      sessionFactory = (SessionFactory)factory.getBean("test.sessionFactory");
      Session s = sessionFactory.openSession();
      TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));  
  protected void tearDown() {
     SessionHolder sessionHolder = (SessionHolderTransactionSynchronizationManager.unbindResource(sessionFactory);
     SessionFactoryUtils.closeSessionIfNecessary(sessionHolder.getSession(), sessionFactory)


Some Subversion Annoyances

A few things really bug me about Subversion. Overall, I like it, and will continue to use it, but there have been a few casualties along the way from the CVS-to-Subversion migration:

  • CVSGraph doesn/t work anymore. I know TSVN has a built-in revision viewer, but a) the graphs aren’t as nice, and b) have you ever tried it on a tree with a large number of revisions? Good luck!
  • The Eclipse CVS plugin. Now, I know that you can download SubClipse, but it’s extra effort, and it’s not as mature, or stable, as the built-in CVS equivalent. Plus, you need to download extra dependencies to get it to work.
  • Those nice StatCVS reports won’t work any more, either. Which I found out when a maven:site command bombed out 75% of the way through.

I know these are relatively small issues, and I’m sure they will be resolved in time, but they are annoying.


Ant Subversion Task, Part 2

I have an updated version of the Ant Subversion task that supports commits. Note to self: look at the DAV protocol spec to find out more about what is actually going on under the hood. The one snag I hit is that you won’t always have a valid Repository instance (e.g. when committing), as the SVN root is read from the administrative files, so you need to handle the credential passing explicitly for this case. Here’s a sample build.xml showing what works now:

<?xml version=”1.0″?>

<project name=”svn” default=”main” basedir=”.”>

<property name=”foo” value=”bar”/>

<target name=”main”>
<taskdef name=”svn” classname=”” />
<delete dir=”c:/temp/sample” failonerror=”false”/>

<svn command=”checkout” svnRoot=”http://localhost/svn/sample/trunk” dest=”c:/temp/sample” revision=”10″ verbose=”true”/>

<svn command=”update” dest=”c:/temp/sample” verbose=”true”/>

<svn command=”commit” dest=”c:/temp/sample” verbose=”true” message=”hello ${foo}”/>


The code is here:

I must say, I’m really impressed with the JavaSVN library – it looks pretty powerful.