许多到许多额外的列的NHibernate [英] many-to-many with extra columns nhibernate

查看:154
本文介绍了许多到许多额外的列的NHibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个表用户。这些表(在数据库中)喜欢用 UGlink 链接表。现在除了主 - 外键链接表有一个额外的列:日期

So I have two tables Users and Groups. These tables are (in the database) liked with a UGlink link table. Now except for the primary-foreign keys the link table have an extra column: Date.

据我所知,这意味着我必须有两个多到一,以中间的链接。

From what I understand this means that I have to have two Many-to-One, with the link "in the middle".

不过,因为我几乎从来不感兴趣的额外的列值,反正是有避免的联系?这就是我希望能够写:

However since I'm almost never is interested of the extra column value, is there anyway to avoid the link? That is I want to be able to write:

thisUser.Groups

以获得代替的基团,

to get the groups, instead of:

thisUser.UGlinks.Group

推荐答案

多到许多,无配对表作为一个实体的显式映射 - 是在NHibernate的当然支载。因此,在情况下,在日期的列自动生成,或可为空的(不必通过应用程序/ NHiberante插入)的,我们能做到喜欢这里:<一HREF =htt​​p://nhforge.org/doc/nh/en/index.html相对=nofollow> 6.8。双向关联

The many-to-many, without the explicit mapping of the pairing table as an entity - is in NHibernate of course suported. So, in case, that the Date column is autogenerated, or nullable (does not have to be inserted by app/NHiberante), we can do it like here: 6.8. Bidirectional Associations

<class name="User">
    <id name="Id" column="Uid"/>
    ...
    <bag name="Groups" table="UGlink" lazy="true">
        <key column="Uid"/>
        <many-to-many class="Group" column="Gid"/>
    </bag>
</class>

<class name="Group">
    <id name="id" column="Gid"/>
    ...

    <!-- inverse end -->
    <bag name="Users" table="UGlink" inverse="true" lazy="true">
        <key column="Gid"/>
        <many-to-many class="User" column="Uid"/>
    </bag>
</class>

那么,我们有什么是一个映射,其中NHiberante不关心配对表,我们可以这样做:

So, what we have is a mapping, in which NHiberante does care about the pairing table, and we can do:

thisUser.Groups

但是,如果我可以建议,不要用去多到许多。该多到一与配对对象(我说)更好的解决方案,因为它会支持组,反之亦然搜索用户。

But if I could suggest, do not go with many-to-many. The many-to-one with pairing object is (I'd say) better solution, because it will support searching Users by Groups and vice versa.

请参阅第24章最佳实践,举:

不要使用复杂的关联映射。

良好的用例是罕见的。大多数时候,你需要存储在链接表的更多信息。在这种情况下,它是最好使用两个一一对多关联到一个中间链路类。事实上,我们认为大多数协会是一个一对多和多对一的,你应该使用其他任何关联的风格的时候要小心,问问自己,是否真的neccessary。

Good usecases for a real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, we think that most associations are one-to-many and many-to-one, you should be careful when using any other association style and ask yourself if it is really neccessary.

下面是一些更详细的解释如何做到这一点没有太多的一对多: NHibernate的:如何重新present多对一对多带一个一对多的关系的关系?

Here is some more detailed explanation how to do it without many-to-many: Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

这篇关于许多到许多额外的列的NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆