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>

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

Reply via email to