I have the following mapping(only relevant properties)
<class name='SpatialItem' lazy='false' table='SpatialItems'>
<many-to-one name="Owner" class="SpatialItem" fetch="join"
lazy="false" not-found="ignore"></many-to-one>
<bag name="Children" lazy="true" cascade="all-delete-orphan">
<key column="Parent" foreign-key="FK_SpatialItemRelation_Parent"/
>
<one-to-many class="SpatialItemRelation"/>
</bag>
<bag name="Parents" lazy="true" cascade="all-delete-orphan">
<key column="Child" foreign-key="FK_SpatialItemRelation_Child"/>
<one-to-many class="SpatialItemRelation"/>
</bag>
</class>
<class name='SpatialItemRelation' lazy='false'
table='LayerProjectRelations'>
<many-to-one name="Child" class="SpatialItem"/>
<many-to-one name="Parent" class="SpatialItem"/>
</class>
Basically it means that each SpatialItem can have a bag of child items
and bag of parent items. Each Item also may have an owner Item.
Lets say I receive a request to delete Item1(which has Item2 as owner)
and Item2.
I tried to delete them in straightforward way:
using(var s = OpenSession())
using(var tx = s.BeginTransaction())
{
foreach(item in itemsToDelete)
s.Delete(item)
tx.Commit();
}
And I got the NonUniqueObjectException, since when deleting Item1,
Item2 got associated with current session.
I can not use session.Merge before deleting, since it creates new
item, so I will be forced to traverse the whole graph of items,
children, parents and owners to replace old item instance with the new
instance. For now I used session.Lock(item, LockMode.None) for each
item in itemsToDelete before issuing the Delete command.
Is this a correct way? It does not look as intended usage of the Lock
function. On the other hand i do not want to delete each item in
separate transaction.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---