存储没有显式 ID 的简单关系
所以,我有两个表,分别称为“用户”和“项目”。我希望每个用户都能够标记(就像“盯着”Google 宇宙中的东西)任何项目。
现在,从集合论的角度来看,将其存储为用户和项目之间的简单关系应该是合理的。将此新表命名为 UserItemFlags。该表应该有两列;一个具有 User 表的外键,另一个具有 Item 表的外键。如果用户 U 标记了项目 I,则这由 UserItemFlags 表中存在的行 (U, I)
表示。
现在,我在 Fluent-NHibernate 中表达这一点的问题是:我似乎无法理解如何处理 Id
映射。理想情况下,我不会有 ID,因为除了用户和项目之间存在或不存在这种关系之外,没有什么其他的了。该模型的一个自然结果是表中不可能有重复的行。这是一个理想的功能。
using FluentNHibernate.Mapping;
public class UserItemFlagsMapping : ClassMap<UserItemFlags>
{
public UserItemFlagsMapping()
{
// Aaaa! I must have an Id!
References(x => x.User).Not.Nullable();
References(x => x.Item).Not.Nullable();
}
}
我很高兴收到任何正确方向的指示,即使他们会要求我在这个特定情况下放弃 Fluent。
So, I have two tables, call them User and Item. I want each user to be able to flag (like "staring" things in the Google universe) any item.
Now, from a set-theoretic perspective it should be reasonable to store this as a simple relation between users and items. Call this new table UserItemFlags. The table should have two columns; one with foreign keys to the User table, the other with foreign keys to the Item table. If user U flags item I, this is then represented by the presence of the row (U, I)
in the UserItemFlags table.
Now, my problem with expressing this in Fluent-NHibernate is this: I can't seem to understand what to do with the Id
mapping. Ideally, I would have no ID, because there is nothing more to it than presence or absence of this relation between a User and an Item. A natural consequence of this model is that it's impossible to have duplicate rows in the table. This is a desired feature.
using FluentNHibernate.Mapping;
public class UserItemFlagsMapping : ClassMap<UserItemFlags>
{
public UserItemFlagsMapping()
{
// Aaaa! I must have an Id!
References(x => x.User).Not.Nullable();
References(x => x.Item).Not.Nullable();
}
}
I'd be happy for any pointers in the right direction, even if they would require me to abandon Fluent for this specific case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说你没有身份证是什么意思?您有一个由用户 ID 和项目 ID 组成的复合 ID。
编辑:答案就在这条评论中:
What do you mean you don't have an ID? You have a composite ID right there of both user ID and item ID.
Edit: The answer is in this comment:
我还没有使用 Fluent NHibernate,但使用标准 NHibernate 通常我不会像这样直接映射表。
多对多表只是在关系的两侧创建为集合。
所以你将拥有:
将元素添加到集合中将在映射表 UserItemFlags 内创建行。
参见:
http://marekblotny.blogspot.com/2009/02/fluence -nhbernate-and-collections.html
大卫
I don't have used yet Fluent NHibernate but with standard NHibernate usually I don't directly map table like this.
Many-to-many tables are just created as collection on the two side of the relationship.
So you will have:
Adding element to the collection will create rows inside the mapping table UserItemFlags.
See also:
http://marekblotny.blogspot.com/2009/02/fluent-nhbernate-and-collections.html
Davide