Nhibernate:如何用一对多关系表示多对多关系? [英] Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

查看:162
本文介绍了Nhibernate:如何用一对多关系表示多对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上阅读了一篇帖子(我再也找不到该帖子了),可以用一对多关系代替多对多关系.有人可以提供示例吗?

I have read a post on the internet (I can no longer find that post for me to refeence) that a Many-To-Many relationship can be replaced with a one-to-many relationship. Can someone provide an example?

推荐答案

我刚想到这个问题,并且意识到,缺少任何答案.很遗憾,尽管我经常指出此NHibernate文档声明: 24.最佳做法

I just come up to that question, and realized, that there is missing any answer. And it is a shame, while I do often point out this NHibernate documentation statement: 24. Best Practices

请勿使用外来关联映射.

真正的多对多关联的好用例很少.大多数 您需要在链接表"中存储其他信息的时间. 在这种情况下,最好使用两个一对多关联来 中间链接类.实际上,我们认为大多数协会 是一对多和多对一的,使用任何 其他联想风格,问问自己是否真的有必要.

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.

看看 23.2下的示例.作者/作品.摘录,AuthorWork之间的多对多关系的简化版本:

Take a look at the example under the 23.2. Author/Work. Extract, the simplified version of the many-to-many relation between Author and Work:

<class name="Work" table="works" ...>
        <id name="Id" column="id" generator="native" />
        ...
        <set name="Authors" table="author_work" lazy="true">
            <key>
                <column name="work_id" not-null="true"/>
            </key>
            <many-to-many class="Author">
                <column name="author_id" not-null="true"/>
            </many-to-many>
        </set>
</class>

及其众多目标作者:

<class name="Author" table="authors">
  ...
  <set name="Works" table="author_work" inverse="true" lazy="true">
     <key column="author_id"/>
     <many-to-many class="Work" column="work_id"/>
  </set>
</class>

因此,如果我们想订购一组有负载的Works,则确实有问题.配对表中没有列.但是更重要的是,没有办法管理这样的列.

So, if we would like to order the set of Works on load, we do have a problem. There is no column in the pair table. But what's more important, there is no way how to manage such a column.

我们可以做的是引入Pair对象:AuthorWork并根据需要扩展Pair表.

What we can do, is to introduced the Pair object: AuthorWork and extend the Pair table as needed

public class AuthorWork
{

    public virtual Author Author { get; set; }
    public virtual Work Work { get; set; }
    public virtual int OrderBy { get; set; }
}

AuthorWork的映射

Mapping of the AuthorWork

<class name="AuthorWork" table="author_work">
    ...
    <many-to-one name="Author" column="author_id" />
    <many-to-one name="Workr"  column="work_id" />
    <property name="OrderBy" />

有了这个,我们可以将多对多映射转换为一对多映射,例如Authors集合:

Having this we can convert the many-to-many mapping to one-to-many, for example the Authors collection:

<set name="Authors" lazy="true"
  order-by="OrderBy">
  <key column="work_id" not-null="true"/>
  <one-to-many class="AuthorWork" />
</set>

我们可以管理实体AuthorWork,设置OrderBy列,从而有效地使用配对表.

And we can manage the entity AuthorWork, set the OrderBy column, and therefore effectively work with the pairing table.

注意:必须同意文档中的建议.要求越多,我们对如何管理关系就越感到满意!

这篇关于Nhibernate:如何用一对多关系表示多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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