Question

I use NHibernate 3.2 with MS SQL Server 2008 R2

I have the fallowing mappings

<class name="LocalizedProperty" table="LocalizedProperty">
   <cache usage="read-write"/>
   <id name="Id" column="Id">
      <generator class="guid.comb"/>
   </id>
   <property name="CultureName"  not-null="true"/>
   <property name="PropertyName"  not-null="true"/>
   <property name="PropertyValue"  not-null="true"/>

   <any id-type="Guid" name="Entity">
      <column name="LocalizedEntityClass"  not-null="true"/>
      <column name="EntityId"  not-null="true"/>
   </any>
</class>

And this one has a Reference to LocalizedProperty:

<class name="CommunicationType" table="CommunicationType" lazy="false"  >
...
<set name="LocalizedProperties" where="LocalizedEntityClass = 'Prayon.Entities.CommunicationType'" cascade="delete">
  <key column="EntityId" foreign-key="none" />
  <one-to-many class="LocalizedProperty" />
</set>
</class>

My Problem is, when I delete an entity of CommunicationType, NHibernate is executing the fallowing update-statement for LocalizedProperty

UPDATE LocalizedProperty SET EntityId = null WHERE EntityId = @p0 AND (LocalizedEntityClass = 'Prayon.Entities.CommunicationType')

Instead of a Delete-Statement.

Does someone see, what is wrong?

Was it helpful?

Solution

To delete child elements of a table you need to specify cascade="all-delete-orphan".

<bag name="TableClassName" table="TableClassName" cascade="all-delete-orphan" >
    <key column="PrimaryKey"/>
    <one-to-many class="NameSpace.TableClassName" />

OTHER TIPS

If you put a all-delete-orphan the problem is the all part. It will cascade all the actions, not just the delete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top