Guys,
I just found out the answer.
Here is the family:

    public class Family : Entity
    {
        public Family()
        {
            Members = new List<Person>();
        }
        public virtual IList<Person> Members { get; set; }
        public virtual Person Father { get; set; }
        public virtual string Name { get; set; }
        public virtual Person Mother { get; set; }
    }

Here is the Person:

    public class Person : Entity
    {
        public virtual string Name { get; set; }
        public virtual Family Family { get; set; }
    }



There is a catch on the mapping. If you simply set the relationships you end 
up getting an exception, it kind of happens a circular reference and 
NHibernate gives you this: 
NHibernate.PropertyValueException: not-null property references a null or 
transient Family.Father

The catch is that you have to map the Father property as nullable. This way 
NHibernate first inserts the family with a null value for father, then 
inserts the father Person, then updates the Family with the Person id.

This is the mapping I ended up with:

    public class FamilyMap : ClassMap<Family>
    {
        public FamilyMap()
        {
            Id(x => x.Id).GeneratedBy.Assigned();
            Map(x => x.Name).Not.Nullable();
            References(x => x.Father).Nullable();
            References(x => x.Mother).Nullable();
            HasMany(x => x.Members).Inverse().Cascade.AllDeleteOrphan();
        }
    }

    public class FamilyMap : ClassMap<Family>
    {
        public FamilyMap()
        {
            Id(x => x.Id).GeneratedBy.Assigned();
            Map(x => x.Name).Not.Nullable();
            References(x => x.Father).Nullable();
            References(x => x.Mother).Nullable();
            HasMany(x => x.Members).Inverse().Cascade.AllDeleteOrphan();
        }
    }


And a simple test:

        [TestMethod]
        public void CanCreateFamilyWithFatherAndMother()
        {
            //arrange
            var family = new Family { Id = 1, Name = "Bassi" };
            var father = new Person { Id = 100, Name = "John" };
            var mother = new Person { Id = 101, Name = "Mary" };
            //set father as member:
            family.Members.Add(father);
            family.Members.Add(mother);
            father.Family = family;
            mother.Family = family;
            //set father as the family father
            family.Father = father;
            //set mother as the family mother
            family.Mother = mother;
 
            //act
            _session.Save(family);
            _session.Flush();
 
            //assert
            _session.Clear();
            var familyPersisted = _session.Get<Family>(1);
            Assert.AreEqual(2, familyPersisted.Members.Count);
            Assert.IsNotNull(familyPersisted.Father);
            Assert.IsNotNull(familyPersisted.Mother);
        }


Cheers,

Giovanni Bassi


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