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

On Aug 26, 2:45 pm, MattO <[email protected]> wrote:
> 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.
>
> On Aug 26, 2:10 pm, Jacob Madsen <[email protected]> wrote:
>
>
>
> > Hi again,
>
> > Should I provide more information, so its easier to spot the issue?
>
> > On Thu, Aug 26, 2010 at 3:15 PM, Jacob Madsen <[email protected]> wrote:
>
> > > 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>- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

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