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
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>
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.
Your example is a bit vague, but you should look into using an Association Class.
精彩评论