Hi there,

I have written 3 tests to show my issue.

Test 1 is failing and I would like to hear if its possible to get this
working like in Test 3. Test 2 is also failing, but this is expected.

I have the following object model. "Entity" is an aggregate root so
"Element" will not be updated/saved/deleted directly (I know according to
aggregate root pattern that its wrong invoke entity.Elements.Clear()
directly as I do below - its an object model with the purpose of simplicity
in this scenario).

public class Entity
{
    public int Id { get; set; }

    IList<Element> Elements { get; set; }

    public Entity()
    {
        Elements = new List<Element>();
    }
}

public class Element
{
    public int Id { get; set; }
}

Test 1:
-------------------------------------------------------------------------------------------------------------------
The following test is performed when cascade="all-delete-orphan"
inverse="true" is set for the bag.

int entityId = 0;

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = new Entity();
    entity.Elements.Add(new Element());

    entityId = (int)session.Save(entity);

    tx.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = session.Get<Entity>(entityId);

    Assert.That(entity.Elements.Count, Is.EqualTo(1)); // Fail, the count is
equal to 0!!! I can see in the Element row data that the FK is NULL.
}

Test 2:
-------------------------------------------------------------------------------------------------------------------
The following test is performed when cascade="all-delete-orphan"
inverse="false" is set for the bag.

int entityId = 0;

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = new Entity();
    entity.Elements.Add(new Element());

    entityId = (int)session.Save(entity);

    tx.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = session.Get<Entity>(entityId);

    Assert.That(entity.Elements.Count, Is.EqualTo(1)); // Success

    entity.Elements.Clear();

    tx.Commit(); //
Exception: NHibernate.ObjectDeletedException : deleted object would be
re-saved by cascade (remove deleted object from associations)
}

Test 3:
-------------------------------------------------------------------------------------------------------------------
The following test is performed when cascade="all-delete-orphan"
inverse="true" is set for the bag.

int entityId = 0;

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = new Entity();

    entityId = (int)session.Save(entity);

    tx.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = session.Get<Entity>(entityId);

    entity.Elements.Add(new Element());

    session.Update(entity);

    tx.Commit();
}

using (var session = sessionFactory.OpenSession())
using (var tx = session.BeingTransaction())
{
    var entity = session.Get<Entity>(entityId);

    Assert.That(entity.Elements.Count, Is.EqualTo(1)); // Success
}

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