views:

89

answers:

2

I have a person class, then have a family class where I have a property Father and a property Mother of type Person.

I have a database table for Person and a Family table containing FamilyId, FatherId, MotherId where FatherId and MotherId is foreign keys for PersonId in Person table.

How would you go about to map this in NHibernate?

+1  A: 

Maybe you have good reasons for your design in the given context.

In theory, you're often better off with a more loosely coupled, flexible, role-based design.

By that I mean that a person is a «Party», and father and mother are «Role»s that a person can play (other roles for a «Party» might be employee, customer, friend, and so forth).

I personally like Peter Coad's DNC pattern in order to solve this particular design problem. An article can be found here: http://edn.embarcadero.com/article/32543

Some years ago, I worked on a large ERP system where I introduced DNC in a C# + NHibernate context, so I know it works in practice as well ;-)

For an in-depth analysis of the role pattern/archetype, have a look at the book Enterprise patterns and MDA: building better software with archetype patterns and UML.

There's also ongoing research in order to solve this problem at the language level, rather than using a pattern, called DCI: http://www.artima.com/articles/dci_visionP.html

Martin R-L
@Martin thanks for great info, will look into it
adriaanp
+2  A: 

This mapping expresses your tables.

public class Family
{
    public virtual int Id { get; set; }
    public virtual Person Mother { get; set; }
    public virtual Person Father { get; set; }
}

<class name="Family">
    <id name="Id" column="FamilyId">
        <generator class="native" />
    </id>
    <many-to-one name="Mother" column="MotherId" />
    <many-to-one name="Father" column="FatherId" />
</class>
Lachlan Roche