如何创建NHibernate的HasManyToMany关系 [英] How to create NHibernate HasManyToMany relation

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

问题描述

我知道大约有HasManyToMany问题,但这次我想把几个字段到像'说明,CreationDate'中间表。

I know there are questions about HasManyToMany but this time I want to put couple fields into middle table like 'Description, CreationDate'.

有关我的情况,我不想绑定双向。我有公司,个人和地址表。
而且每个公司或个人可能有超过1个地址。
在这种情况下我该怎么办?
我应该怎么写的类和映射的code?

For my situation I don't want to bind two way. I have company, person and address tables. And every company or person may have more than 1 address. In this situation what should I do? How should I write the code of classes and mappings?

下面你可以看到表:

推荐答案

在这种情况下,答案是pretty简单。不要使用许多一对多。使用配对对象。究竟对你提到的原因:扩展的配对对象,具有多个属性:

In this case the answer is pretty simple. Do not use many-to-many. Use pairing object. Exactly for the reasons you've mentioned: Extend the pairing object with more properties:

这里检查 24。最佳实践的,一个引用:

Check here 24. Best Practices, a cite:

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

Don't use exotic association mappings.

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.

换句话说,创建一到不少来自boht结束,关系闯民宅的配对对象多到一从配对对象。

Other words, create the one-to-many relations refering the pairing objects from boht ends, and many-to-one from the pairing object.

也检查这些:

  • Hibernate: many-to-many relationship table as entity
  • NHibernate Bidirectional Many-to-Many Mapping List / Bag
  • Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

地址和公司的一个例子。首先配对对象

An example of the Address and Company. First Pairing object

public class AddressCompany
{
    // the relation to both sides
    public virtual Address Address { get; set; }
    public virtual Company Company { get; set; }

    // many other settings we need
    public virtual string   Description  { get; set; }
    public virtual DateTime CreationDate { get; set; }
    ...
}

在坚果壳的地址和公司名称:

the Address and Company in a nut-shell:

public class Address
{
    public virtual IList<AddressCompany> Companies { get; set; }
    ...
}

public class Company
{
    public virtual IList<AddressCompany> Addresses { get; set; }
    ...
}

如预期的映射:

public AddressMap()
{
    HasMany(x => x.Companies)
     ...
}
public CompanyMap()
{
    HasMany(x => x.Addresses)
     ...
}
public AddressCompanyMap()
{
    References(x => x.Address)..
    References(x => x.Company)..
     ...
}

所以,这是重新presenting的配对对象

So, this is representing the Pairing object

好了,但现在我们可以找到一些公司日期之后创建的:

Well, but now we can find some Companies Created after a date:

var subquery = QueryOver.Of<AddressCompany>()
    .Where(c => c.CreationDate > new DateTime(2000, 1, 1))
    .Select(c => c.Company.ID);

var query = session.QueryOver<Company>()
    .WithSubquery
    .WhereProperty(c => c.ID)
    .In(subquery)
    ...;

这样我们也可以在地址过滤公司...

This way we can also filter Company over the Address...

这篇关于如何创建NHibernate的HasManyToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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