views:

32

answers:

1

Dearest All,

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  A: 

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".

Kendrick
@Kendrick is correct. Use All instead of AllDeleteOrphan.
Diego Mijelshon