Hi
I am confused about how NHibernate treats orphan objects and would like 
some clarification on this subject. Please see below for my simple test 
case. When I run this test case I expect the child object "sequence" to be 
deleted from the DB but this isn't the case, instead I am left with an 
orphan object. The documentation on many-to-one, 
http://www.nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone, 
states that the cascade option:

     "Specifies which operations should be cascaded from the parent object 
to the associated object"

To be honest, I'm not sure if this means that my delete scenario is 
supported or if the cascade option only comes into play when the parent 
object is deleted.

So to summarise my questions are:

   1. Does NHibernate support many-to-one deletes as in the unit test, and 
   if so what is wrong with my test case.
   2. If NHibernate does not support this scenario what is the logic behind 
   not supporting it.
   3. If NHibernate does not support this scenario is there a work around 
   so child objects can be deleted by their parents from within the domain 
   model and be deleted within the DB.


Thanks in advance

Michael


NB. I have also tried using one-to-one and described by Ayende (
http://ayende.com/blog/3960/nhibernate-mapping-one-to-one) with the same 
results.

--------------------------------Class 
declarations---------------------------------------------

public class Host
{
    public virtual int id { get; set; }
    public virtual Sequence sequence { get; set; }
}

public class Sequence
{
    public virtual int id { get; set; }
}

--------------------------------XML 
declarations---------------------------------------------

<class name="Host">
    <id name="id">
        <generator class="native" />
    </id>
    <many-to-one name ="sequence" cascade="all" />
</class>

<class name="Sequence">
    <id name="id">
        <generator class="native" />
    </id>
</class>

--------------------------------Unit 
Test---------------------------------------------

[TestFixture]
public class Fixture : BugTestCase
{
    [Test]
    public void CanDeleteChild()
    {
        Host host = new Host();
        Sequence sequence = new Sequence(); ;
        host.sequence = sequence;

        using (var s = OpenSession())
        {
            using (s.BeginTransaction())
            {
                s.SaveOrUpdate(host);
                s.Transaction.Commit();
            }
            s.Close();
        }

        int sequence_id = sequence.id;

        using (var s = OpenSession())
        {
            using (s.BeginTransaction())
            {
                sequence.host = null;
                sequence = null;
                host.sequence = null;
                
                s.SaveOrUpdate(host);
                s.Transaction.Commit();
            }
            s.Close();
        }

        using (var s = OpenSession())
        {
            Assert.IsNull(s.Get<Sequence>(sequence_id));
            sessions.Close();
        }    
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/ndaP2DBeEwMJ.
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