具有中间表的NHibernate映射

具有中间表的NHibernate映射,nhibernate,nhibernate-mapping,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<Pe

我是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
}
公共类人物
{
字符串名;
智力年龄;
IList Children;//这是所有人的孩子列表
}
我该怎么设置这个呢?我完全迷路了,似乎找不到任何相关的例子


谢谢

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

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

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

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

这应该让您开始:

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

这应该让您开始:

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


您使用relationId的目的是什么?您使用relationId的目的是什么?是的,看起来像是多对多。只是碰巧双方都是同一张桌子。是的,看起来像是多对多。碰巧双方都是同一张桌子。谢谢,这正是我想要的。谢谢,这正是我想要的。