NHibernate bag did not delete records when IList.Clear() was called

By ramon
September 16, 2009

I was having a problem where NHibernate did not automatically delete childs if a collection was emptied by calling IList.Clear() like in the following code example:

var s = GetSession();
var parent = s.Get<Parent>(1);

What did work ofcourse was code like the following before executing clear which marked the records for deletion and NHibernate executed the correct delete statements when the session was closed or flushed.

foreach(var c in parent.Childs) s.Delete(c);

I searched the internet for quite a while and playing around with the cascade and inverse attributes in the .hbm files as I knew it had be an error in the configuration.

<bag name="Topics" cascade="all" inverse="true">
	<key column="Webcast_Id"/>
	<one-to-many class="AddonIndex"/>

After searching for quite some time I found that the problem was cascade=”all” which should have been cascade=”all-delete-orphan” and when I read that on a forum I had a very bigy WTF moment. I *really* assumed that all would do the deletion of the orphans as that is what the keyword implies, that it does *all* while in reality it does all except deletion of orphans.

So I would like to suggest the (N)Hibernate team to change the names of the cascade values or just ditch the all value.

Comments: 0

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta