NHibernate.ObjectDeletedException : deleted object would be re-saved by cascade (remove deleted object from associations)[Tests.CollectionElement#2]

3,139 views
Skip to first unread message

Jacob Madsen

unread,
Aug 26, 2010, 9:15:16 AM8/26/10
to nhusers

Hi there,

Why do I get the following exception:
NHibernate.ObjectDeletedException : deleted object would be re-saved
by cascade (remove deleted object from associations)
[Tests.CollectionElement#2]

I want orphans deleted when I invoke Clear() on the collection owning
them.


///// Test start here
var collectionOwnerId = 0L;

using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
collectionOwnerId = (long) session.Save(new CollectionOwner());

var collectionOwner =
session.Get<CollectionOwner>(collectionOwnerId);

collectionOwner.FirstCollection.Add(new CollectionElement("First
element"));

transaction.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var collectionOwner =
session.Get<CollectionOwner>(collectionOwnerId);

collectionOwner.FirstCollection.Clear();

transaction.Commit(); // Exception here!!!
}
///// Test end here


public class CollectionOwner : ReferenceRootObject<CollectionOwner>
{
public IList<CollectionElement> FirstCollection { get; private
set; }

public CollectionOwner()
{
FirstCollection = new List<CollectionElement>();
}
}


public class CollectionElement : ReferenceObject<CollectionElement>
{
public string Name { get; private set; }

public CollectionElement()
{
Name = "";
}

public CollectionElement(string name)
{
Name = name;
}
}


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="property" auto-import="true" default-cascade="save-update"
default-lazy="false">
<class xmlns="urn:nhibernate-mapping-2.2" lazy="false"
mutable="true" name="Tests.CollectionOwner, Tests, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="collection_owners">
<id name="Id" type="System.Int64, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="hilo">
<param name="max_lo">0</param>
</generator>
</id>
<bag access="backfield" cascade="all-delete-orphan"
name="FirstCollection" mutable="true">
<key>
<column name="CollectionOwner_id" />
</key>
<one-to-many class="Tests.CollectionElement, Tests,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="property" auto-import="true" default-cascade="save-update"
default-lazy="false">
<class xmlns="urn:nhibernate-mapping-2.2" lazy="false"
mutable="true" name="Tests.CollectionElement, Tests, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="collection_elements">
<id name="Id" type="System.Int64, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="id" />
<generator class="hilo">
<param name="max_lo">0</param>
</generator>
</id>
<property name="Name" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" length="255" not-null="true" />
</property>
</class>
</hibernate-mapping>

Jacob Madsen

unread,
Aug 26, 2010, 3:10:50 PM8/26/10
to nhusers
Hi again,

Should I provide more information, so its easier to spot the issue?

MattO

unread,
Aug 26, 2010, 3:45:19 PM8/26/10
to nhusers
Clear just removes the object from the session cache as far as I
know. it doesn't do any database deletion.

You state the following: I want orphans deleted when I invoke Clear()
on the collection owning
them.

But you are issueing clear on the child collection, not the collection
that is owning them. Regardless, the clear statement I don't think is
what you want to use.

Instead you want to use Delete, and you want to do it on the parent
object (collectionOwner), and that will delete the children
(FirstCollection) associated with the parent object by cascading the
deletes.
> > </hibernate-mapping>- Hide quoted text -
>
> - Show quoted text -

MattO

unread,
Aug 26, 2010, 3:56:27 PM8/26/10
to nhusers
Also, look at this, maybe this is what you are trying to do. In this
case the suggestion was to set the inverse="true" property on the
correct side of the relationship.

http://stackoverflow.com/questions/302720/how-to-delete-child-object-in-nhibernate

So I believe setting it as follows might fix it:

<bag access="backfield" cascade="all-delete-orphan"
name="FirstCollection" mutable="true" inverse="true">
> > - Show quoted text -- Hide quoted text -

Jacob Madsen

unread,
Aug 28, 2010, 4:11:00 AM8/28/10
to nhu...@googlegroups.com
Thank you Matt. It was the missing piece of information I needed :-)

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Reply all
Reply to author
Forward
0 new messages