1
votes

I have a one-directional many-to-many association: the ListDefinition class has the Columns property of type IList, while a column can be part of several ListDefinition-s. The problem is, whenever I try to remove a column from one Columns collection (without deleting it or removing from other ListDefinitions), I'm getting this error:
deleted object would be re-saved by cascade (remove deleted object from associations)[Domain.Lists.Definitions.ListColumnDefinition#2]

My mapping is generated via Fluent NH:

mapping.HasManyToMany(list => list.Columns)
.AsList(part => part.Column("`index`"))
.Cascade.AllDeleteOrphan()

Here's the generated hbm:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="all" default-lazy="false">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Domain.Lists.Definitions.ListDefinition, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="ListDefinition">
<id access="nosetter.camelcase-underscore" name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<list cascade="all-delete-orphan" name="Columns" table="ListColumnDefinitionToListDefinition" mutable="true">
<key>
<column name="ListDefinition_id" /> </key>
<index>
<column name="index" />
</index>
<many-to-many class="Domain.Lists.Definitions.ListColumnDefinition, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="ListColumnDefinition_id" />
</many-to-many>
</list>

</class> </hibernate-mapping>

1
Not sure if I understand what you're doing, Could you paste the relevant code? (how you delete the entity that is throwing this error)Claudio Redi
I'm not deleting an entity. I just do this: listDefinition.Columns.Remove(column);ulu

1 Answers

1
votes

Are you sure "all-delete-orphan" is what you want?

From Section 21.3. Cascading life cycle of the above link:

In our case, a Child cannot exist without its parent. So if we remove a Child from the collection, we do want it to be deleted. To do this, we must use cascade="all-delete-orphan".