通过将表与 NHibernate 连接来映射元素

发布于 2024-10-09 19:26:22 字数 919 浏览 0 评论 0原文

这是我以前做过很多次的事情,但我现在脑子一片空白,我会尝试简单地概述一下我目前的情况。

我目前有 3 个表,如下所示:

Office > id, name
Person > id, name
Office_Personnel > office_id, person_id

然后我有一个 Person(id,name)和 Office 的模型,但是 Office 模型包含人员信息:

public class Office
{
 int Id {get;set;}
 string Name {get;set;}
 ICollection<Person> Personnel {get;set;}
}

映射人员很容易,但现在我有点困惑为什么 Office 无法正确映射。当我映射人员时,我选择使用一组,因为不应该有任何重复项,但是它似乎没有像我预期的那样工作......

<set name="Personnel" table="office_personnel" cascade="all">
  <key column="office_id" />
  <one-to-many class="Person"/>
</set>

现在让我感到奇怪的一件事是没有任何迹象表明关于什么人应该绑定到(person_id)。它不断尝试在 Person 表中查找 *office_id* 列。

我确信这只是一些简单的问题,我是个白痴,但任何帮助都会很棒!

顺便说一句,我正在权衡是否应该有一个中间人表,因为我可以直接在 Person 表中放置 Office_Id 列,但我不能 100% 确定是否在我的 < em>真实的项目 Person 类可能会在多个办公室中......

This is stuff ive done lots of times before but my mind is just blanking at the moment, i will try and give a simple overview of my current situation.

I currently have 3 tables as shown below:

Office > id, name
Person > id, name
Office_Personnel > office_id, person_id

I then have a model for Person (id, name) and Office, however the Office model contains personnel information:

public class Office
{
 int Id {get;set;}
 string Name {get;set;}
 ICollection<Person> Personnel {get;set;}
}

Mapping person is easy, but now im a bit stumped as to why office wont map properly. I chose to use a set when I was mapping the Personnel as there shouldn't be any duplicates, however it doesn't seem to work as I would expect...

<set name="Personnel" table="office_personnel" cascade="all">
  <key column="office_id" />
  <one-to-many class="Person"/>
</set>

Now one thing that strikes me as odd is that there is no indication as to what person should be binding to (person_id). It keeps trying to find *office_id* column within the Person table.

I'm sure this is just some simple problem and im being an idiot, but any help would be great!

On a side note, I was weighing up if I should even bother having a middle man table, as I could directly put an Office_Id column within the Person table, but im not 100% sure if in my real project the Person class could be in multiple Offices further down the line...

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

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

发布评论

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

评论(1

成熟的代价 2024-10-16 19:26:22

为了正确映射它,您需要有 3 个实体:

Person 1 <-> n Personnel n <-> 1 Office

或者使用连接表将其映射为多对多关联(请参阅 hibernate 文档的第 8.3 节)

 Person n <-> n Office

In order to map this correctly, you need to either have 3 entities:

Person 1 <-> n Personnel n <-> 1 Office

or map it as a many-to-many association using join table (see section 8.3 of the hibernate docs)

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