-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Understanding cascade deletes - PLEASE HELP - going Nuts!
PostPosted: Wed Sep 09, 2009 3:48 am 
Newbie

Joined: Sat Mar 07, 2009 2:24 am
Posts: 9
I am going nuts here trying to resolve a cascading update/delete issue :-)

I have a Parent Entity with a collection Child Entities. If I modify the list of Child entities in a detached Parent object, adding, deleting etc - I am not seeing the updates cascaded correctly to the Child collection.

Mapping Files:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Domain"
                   namespace="Domain">
  <class name="Parent" table="Parent" >

    <id name="Id">
      <generator class="guid.comb" />
    </id>

    <version name="LastModified"
                    unsaved-value="0"
                    column="LastModified"
                     />
   
    <property name="Name" type="String" length="250" />

    <bag name="ParentChildren" lazy="false" table="Parent_Children" cascade="all-delete-orphan" inverse="true">
      <key column="ParentId" on-delete="cascade" />
      <one-to-many class="ParentChildren" />
    </bag>
   
  </class>

  <class name="ParentChildren" table="Parent_Children">

    <id name="Id">
      <generator class="guid.comb" />
    </id>

    <version name="LastModified"
                    unsaved-value="0"
                    column="LastModified"
                     />
   
       <many-to-one
      name="Parent"
      class="Parent"
      column="ParentId"
      lazy="false"
      not-null="true"
       />
   
  </class>
</hibernate-mapping>


Test
Code:
        [Test]
        public void Test()
        {
            Guid id;
            int lastModified;
            // add a child into 1st session then detach
            using(ISession session = Store.Local.Get<ISessionFactory>("SessionFactory").OpenSession())
            {
                Console.Out.WriteLine("Selecting...");
                Parent parent =  (Parent) session.Get(typeof (Parent), new Guid("4bef7acb-bdae-4dd0-ba1e-9c7500f29d47"));
               
                id = parent.Id;
                lastModified = parent.LastModified + 1; // ensure the detached version used later is equal to the persisted version
               
                Console.Out.WriteLine("Adding Child...");
                Child child = (from c in session.Linq<Child>() select c).First();
                parent.AddChild(child, 0m);

                session.Flush();
                session.Dispose(); // not needed i know
            }

            // attach a parent, then save with no Children
            using (ISession session = Store.Local.Get<ISessionFactory>("SessionFactory").OpenSession())
            {
                Parent parent = new Parent("Test");             
               
                parent.Id = id;
                parent.LastModified = lastModified;
               
                session.Update(parent);
                session.Flush();
            }
        }


I assume that the fact that the product has been updated to have no children in its collection - the children would be deleted in the Parent_Child table. The problems seems to be something to do with attaching the Product to the new session? As the cascade is set to all-delete-orphan I assume that changes to the collection would be propagated to the relevant entities/tables? In this case deletes?

What am I missing here?

C


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.