How to delete child object in NHibernate?

Posted by Mark Struzinski on Stack Overflow See other posts from Stack Overflow or by Mark Struzinski
Published on 2008-11-19T17:33:15Z Indexed on 2010/05/27 16:31 UTC
Read the original article Hit count: 673

Filed under:

I have a parent object which has a one to many relationship with an IList of child objects. What is the best way to delete the child objects? I am not deleting the parent. My parent object contains an IList of child objects. Here is the mapping for the one to many relationship:

<bag name="Tiers" cascade="all">
  <key column="mismatch_id_no" />
  <one-to-many class="TGR_BL.PromoTier,TGR_BL"/>
</bag>

If I try to remove all objects from the collection using clear(), then call SaveOrUpdate(), I get this exception:

System.Data.SqlClient.SqlException: Cannot insert the value NULL into column

If I try to delete the child objects individually then remove them from the parent, I get an exception:

deleted object would be re-saved by cascade

This is my first time dealing with deleting child objects in NHibernate. What am I doing wrong?

edit: Just to clarify - I'm NOT trying to delete the parent object, just the child objects. I have the relationship set up as a one to many on the parent. Do I also need to create a many-to-one relationship on the child object mapping?

© Stack Overflow or respective owner

Related posts about nhibernate