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: Cascade="all-delete-orphan"
PostPosted: Mon Nov 16, 2009 10:05 am 
Newbie

Joined: Mon Nov 02, 2009 9:52 am
Posts: 19
I have read all-delete-orphan would delete every orphaned child of the deleted object.
I understood it as the following:
In a parent child context, I have 3 objects, Adam, Peter and Claire. Peter is the child of Adam and Claire is the child of Peter.
If I delete Claire, Peter and Adam are still persistent. If I would delete Peter, only Adam would be still persistent. Does this make sense? Ok, now to my question:

If I delete Claire in my project, NHibernate deletes every single object, that is associated with Claire. I would expect only Claire to be deleted...

My Class:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace NewNHibernate
{
    public class Person
    {
        public virtual int _id { get; set; }
        public virtual String _name { get; set; }
        public virtual Person _parent { get; set; }
        public virtual IList<Person> Children { get; set; }

        public Person()
        { }

        public Person(String name, Person parent)
        {
            _name = name;
            _parent = parent;

            Children = new List<Person>();

            if (null != parent)
            {
                parent.Children.Add(this);
            }
        }

        public virtual bool IsRoot()
        {
            return null == _parent ? true : false;
        }

        public virtual Person GetRoot()
        {
            Person root = _parent;

            while (root._parent != null)
            {
                root = root._parent;
            }

            return root;
        }

        public virtual Person AddChild(Person child)
        {
            child._parent = this;
            Person ancestor = this;

            while (ancestor._parent != null)
            {
                ancestor._parent = ancestor._parent._parent;
            }

            ancestor.Children.Add(child);

            return child;
        }
    }
}


My mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
<class name="NewNHibernate.Person, NewNHibernate">

    <id name="_id" column="id" type="int" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="_name" column="name" type="string" not-null="true" unique="true" />

    <many-to-one name="_parent" column="parent" class="NewNHibernate.Person" cascade="all-delete-orphan"/>

    <bag name="Children" cascade="all-delete-orphan" inverse="true">
      <key column="parent" foreign-key="FK_Person_Children" />
      <one-to-many class="NewNHibernate.Person"/>
    </bag>
 
  </class> 
</hibernate-mapping>


My Program:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace NewNHibernate
{
    class Program
    {
        static void Main(string[] args)
        {
            Person Adam = new Person("Adam", null);
            Person Peter = new Person("Peter", Adam);
            Person Claire = new Person("Claire", Peter);

            Person Nathan = new Person("Nathan", null);
            Person Matt = new Person("Matt", Nathan);

            using (ISession session = OpenSession())
            {
                session.SaveOrUpdate(Claire);
                session.SaveOrUpdate(Matt);

                session.Flush();

                IList<Person> Peters;

                ICriteria crit = session.CreateCriteria(typeof(Person));
                crit.Add(NHibernate.Criterion.Expression.Eq("_name", "Peter"));
                IList<Person> p = crit.List<Person>();

                Console.WriteLine(p.First()._name);

                session.Delete(p.First());
                session.Flush();
            }
        }

        public static ISession OpenSession()
        {
            ISession session = null;

                try
                {
                    log4net.Config.XmlConfigurator.Configure();
                    Configuration cfg = new Configuration().Configure();
                    session = cfg.BuildSessionFactory().OpenSession();
                }
                catch(Exception ex)
                {
                    if (null != ex.InnerException)
                    {
                        Console.WriteLine(ex.InnerException.Message);
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                    return null;
                }
            return session;
        }
    }
}


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.