存储没有显式 ID 的简单关系

发布于 2024-10-13 03:32:26 字数 756 浏览 3 评论 0原文

所以,我有两个表,分别称为“用户”和“项目”。我希望每个用户都能够标记(就像“盯着”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 技术交流群。

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

发布评论

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

评论(2

我爱人 2024-10-20 03:32:26

你说你没有身份证是什么意思?您有一个由用户 ID 和项目 ID 组成的复合 ID。


编辑:答案就在这条评论中:

Id() 方法仅适用于单列主键。您想使用 CompositeId() 代替。类似于以下内容: CompositeId().KeyReference(x => x.User, "user_id").KeyReference(x => x.Item, "item_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:

The Id() method is only meant for single column primary keys. You want to use CompositeId() instead. Something like the following: CompositeId().KeyReference(x => x.User, "user_id").KeyReference(x => x.Item, "item_id"; Obviously, if you're using default convention mapping, you don't need the column names.

放飞的风筝 2024-10-20 03:32:26

我还没有使用 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:

User.Items and Item.Users

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

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