NHibernate 与中间表的映射

发布于 2024-09-15 19:43:27 字数 472 浏览 4 评论 0原文

我是 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
}

我将如何设置它?我相当迷失,似乎找不到任何相关的例子。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后咖啡店 2024-09-22 19:43:27

这应该可以帮助您开始:

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

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>
预谋 2024-09-22 19:43:27

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

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.

神魇的王 2024-09-22 19:43:27

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文