Best of Xebia tech list: Hibernate Cascade Question

Erwin van der Koogh

I am sorry I am a bit behind on posting the techlist questions, but I have been a bit busy lately with tons of different projects.
This time we have a hibernate question..

From Lars
Subject Hibernate question
Date August 27, 2007 16:30:26

Hi,

I have a Hibernate question. This is the situation (I ommited rest of annotations etc):

class Organisation {
 @OneToMany(cascade=CascadeType.ALL)
 @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
 private List<AbstractContactDetails> allContactDetails;
}

class AbstractContactDetails {
}

It is a uni-directional one to many.

Now I want to delete the Organisation and that should cascade to the allContactDetails. That's why I added the Hibernate specific annotation "@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)". In my test code I then do:

organisation.setAllContactDetails(null);
organisationDao.update(organisation);
organisationDao.getHibernateTemplate().flush();

(The flush is there so I can test after the last call as Hibernate only synches with the database with an explicit flush or a select).

This results in the following exception:

Caused by: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: domain.Organisation.allContactDetails

I found that a bit strange because I think this is the whole idea behind the all-delete-orphan thing.... I am adding it so Hibernate deletes my orphans and now it complains that it is no longer referenced... Any idea's?

We are using Hibernate 3.2.0.ga and Java 5.

Lars

From Silvester van der Bijl
Subject RE: Hibernate question
Date August 27, 2007 16:33:19

Lars,

As far as I know you shouldn't set the collection to null. Does it work when you just clear the list? Don't now how Hibernate handles this, but I'm guessing it needs a reference to the collection to be able to detect which items should be deleted (the orphans).

Silvester

From Lars Vonk
Subject RE: Hibernate question
Date August 27, 2007 16:38:06

Great, thanks Silvester that indeed solved it.

Lars

Comments (0)

    Add a Comment