Wednesday, July 14, 2010

Fluent NHibernate, One-to-many with Orphan Delete

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Mapping;
using NUnit.Framework;

namespace NStackExample.Data.Tests
{

public class Parent : Entity
{

private ICollection m_Children = new HashSet();

public virtual ICollection Children
{
get { return m_Children; }
protected set { m_Children = value; }
}

}

public class Child : Entity
{

private Parent m_Parent;

public virtual Parent Parent
{
get { return m_Parent; }
set { m_Parent = value; }
}

}

public class ParentMapping : ClassMap
{
public ParentMapping()
{
Id((Parent x) => x.ID).GeneratedBy.GuidComb();
HasMany((Parent x) => x.Children)
.AsSet()
.WithForeignKeyConstraintName("ParentChildren")
.Cascade.AllDeleteOrphan()
.Inverse();
}
}

public class ChildMapping : ClassMap
{
public ChildMapping()
{
Id((Child x) => x.ID).GeneratedBy.GuidComb();
References((Child x) => x.Parent)
.Cascade.All()
.WithForeignKey("ChildParent")
.Not.Nullable();
}
}

[TestFixture()]
public class ParentMappingTests
{

[Test()]
public void CanCascadeSaveFromParentToChild()
{
Guid ID;
Parent P;
Child C;
using (SQLiteDatabaseScope Scope = new SQLiteDatabaseScope())
{
using (ISession Session = Scope.OpenSession())
{
using (ITransaction Tran = Session.BeginTransaction())
{
P = new Parent();

//Add a child of the parent
C = new Child { Parent = P };
P.Children.Add(C);

ID = (Guid) Session.Save(P);
Tran.Commit();
}
Session.Clear();

using (ITransaction Tran = Session.BeginTransaction())
{

P = Session.Get(ID);

Assert.IsNotNull(P);
Assert.AreEqual(ID, P.ID);

Assert.AreEqual(1, P.Children.Count);
Assert.AreNotSame(C, P.Children.First());
Assert.AreEqual(C.ID , P.Children.First().ID );
Assert.AreSame(P.Children.First().Parent, P);

Tran.Commit();

}
}

}
}

[Test()]
public void CanDeleteOrphanFromParentToChildren()
{
Guid ID;
Parent P;
Child C;
using (SQLiteDatabaseScope Scope = new SQLiteDatabaseScope())
{
using (ISession Session = Scope.OpenSession())
{
using (ITransaction Tran = Session.BeginTransaction())
{
P = new Parent();

//Add a child of the parent
C = new Child { Parent = P };
P.Children.Add(C);

ID = (Guid) Session.Save(P);
Tran.Commit();
}
Session.Clear();

using (ITransaction Tran = Session.BeginTransaction())
{

P = Session.Get(ID);

Assert.IsNotNull(P);
Assert.AreEqual(ID, P.ID);

Assert.AreEqual(1, P.Children.Count);
Assert.AreNotSame(C, P.Children.First());
Assert.AreEqual(C.ID, P.Children.First().ID );
Assert.AreSame(P.Children.First().Parent, P);

Tran.Commit();
}
Session.Clear();

//Orphan the child
C = P.Children.First();
P.Children.Remove(C);
C.Parent = null;

using (ITransaction Tran = Session.BeginTransaction())
{
//Orhpaned child should be deleted
Session.SaveOrUpdate(P);
Tran.Commit();
}
Session.Clear();

using (ITransaction Tran = Session.BeginTransaction())
{
P = Session.Get(ID);

Assert.AreEqual(0, P.Children.Count);

Tran.Commit();

}
}
}
}

}

}

No comments:

Post a Comment

5 Strategies for Getting More Work Done in Less Time

Summary.    You’ve got more to do than could possibly get done with your current work style. You’ve prioritized. You’ve planned. You’ve dele...