通过将表与 NHibernate 连接来映射元素
这是我以前做过很多次的事情,但我现在脑子一片空白,我会尝试简单地概述一下我目前的情况。
我目前有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了正确映射它,您需要有 3 个实体:
或者使用连接表将其映射为多对多关联(请参阅 hibernate 文档的第 8.3 节)
In order to map this correctly, you need to either have 3 entities:
or map it as a many-to-many association using join table (see section 8.3 of the hibernate docs)