HibernateSystemException- Don’t change the reference to a collection with cascade=”all-delete-orphan”

13 Feb

This is not an error which I got. This is an error that bugged one of my friend for a few days.

The error is

org.springframework.orm.hibernate3.HibernateSystemException: Don’t change the reference to a collection with cascade=”all-delete-orphan”: com.test.Cat.kittens; nested exception is org.hibernate.HibernateException: Don’t change the reference to a collection with cascade=”all-delete-orphan”: com.test.Cat.kittens on 10.01.2009@15.26.33

<hibernate-mapping package="com.test">
       <class name="Cat" lazy="false">
			   ......
			   ......
			   ......

               <set name="kittens" table="kitten" order-by="kitten_name asc" inverse="true"                             cascade="all-delete-orphan">
               <key column="cat_id"/>
               <one-to-many class="Kitten"/>
           </set>
       </class>
</hibernate-mapping>

We researched a few solutions on the net. The following solutions are based mainly on the two sources
http://forum.springframework.org/showthread.php?t=25809
http://www.hibernate.org/117.html#A3

Since I myself did not get this problem I can’t give much details. And the solutions below may or may not work for you.

There are two ways to solve this problem
i) If posible switch to the latest version of the Hibernate. Even though it is not a bug in the Hibernate, some people reported that somehow this problem doesn’t appear in the new version of hibernate.

Caution: If your application uses Hibernate extensively then you might consider testing the application throughly after upgrade. If you can’t upgrade for some reasons, then read on

ii) If you can’t upgrade then as per the solutions provided on the forums, you should call clear() if you need to remove the collections marked as all-delete-orphan.
Fair enough. Makes sense, since you are cascading the delete you should inform Hibernate if you intend to remove the collection.

For example,
Instead of doing something like this.

	public void removeKittens(){
	  //Load Cat with kittens
	  Cat cat = getHibernateTemplate().get(Cat.class, 1234);
	  cat.setKittens(null);
	  getHibernateTemplate().save(cat);
	}

Do something like this

	public void removeKittens(){
	  //Load Cat with kittens
	  Cat cat = getHibernateTemplate().get(Cat.class, 1234);
	  cat.getKittens.clear();
	  getHibernateTemplate().save(cat);
	}

As I said this is not a bug in Hibernate’s older version. But somehow in the new version, as people reported on the forums, this problem is not there. That means you are not required to call clear(). I am not sure because I have not faced this problem myself.

About these ads

One Response to “HibernateSystemException- Don’t change the reference to a collection with cascade=”all-delete-orphan””

  1. Julie M July 29, 2009 at 4:02 pm #

    In the pojo for Cat , there should be something like
    public void setKittens(Set kittens) {
    this.kittens= kittens;
    }

    Instead of this.kittens = kittens;
    do like :
    this.kittens.clear();
    this.kittens.addAll(kittens);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: