Justin
Justin

Reputation: 2372

NHibernate mapping with an intermediate table

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

Upvotes: 0

Views: 679

Answers (3)

Diego Mijelshon
Diego Mijelshon

Reputation: 52753

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>

Upvotes: 3

Petr Kozelek
Petr Kozelek

Reputation: 1126

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.

Upvotes: 1

Corey Coogan
Corey Coogan

Reputation: 1639

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

Upvotes: 0

Related Questions