NHibernate映射与中间表

时间:2010-08-24 19:27:44

标签: nhibernate nhibernate-mapping

我是NHibernate的新手,并且遇到了一些映射问题。

假设我有一张桌子:

People

PersonID
PersonName
PersonAge

然后我有另一张桌子

ParentRelaitions

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

我真正想要摆脱的是像这样的对象

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

我该如何设置?我很丢失,似乎找不到任何相关的例子。

由于

3 个答案:

答案 0 :(得分:3)

这应该让你开始:

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

答案 1 :(得分:1)

我不明白。父母和孩子之间有什么关系? 1:N还是M:N?如果1:N则研究NHibernate关系many-to-one,如果M:N则研究many-to-many

答案 2 :(得分:0)

您的示例有点模糊,但您应该考虑使用关联类。