views:

30

answers:

3

I am new to NHibernate and am running into some issues with mapping.

Lets say I have a table:

People

PersonID
PersonName
PersonAge

Then I have another table

ParentRelaitions

RelationID
Parent (This is a PersonID)
Child (This is also a PersonID)

What I really want to get out of this is an object like this

public class Person
{
string name;
int age;
IList<Person> Children; //This is a list of all the persons children
}

How would I go about setting this up? I am fairly lost and can't seem to find any relevant examples.

Thanks

A: 

Your example is a bit vague, but you should look into using an Association Class.

Corey Coogan
+1  A: 

I do not understand. What is the relationship between Parent and child? 1:N or M:N? If 1:N then study NHibernate relation many-to-one, if M:N then study many-to-many.

Petr Kozelek
Yep, looks like a many-to-many. It just happens that it's the same table on both sides of the relationship.
Douglas
+2  A: 

This should get you started:

<class name="Person">
  <id column"PersonId" type="...">
    <generator class="..."/>
  </id>
  <property name="name" column="PersonName" access="field"/>
  <property name="age" column="PersonAge" access="field"/>
  <idbag name="Children" table="ParentRelations">
    <collection-id column="RelationId" type="...">
      <generator class="..."/>
    </collection-id>
    <key column="Parent"/>
    <many-to-many column="Child" class="Person"/>
  </idbag>
</class>
Diego Mijelshon
Thanks this is very much what I was looking for.
Justin