本文介绍了我做多对多使用不当连贯NHibernate是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主要实体(DB表)

I have two main entities (db tables)


  1. 项目

  2. 应用

  1. Project
  2. Application

我有个桥叫提交讨论的 ProjectApplication 3 COL(ID,专案编号,的applicationID)

I have a bridge tabled called ProjectApplication with 3 col (Id, ProjectId, ApplicationId)

一个项目可以有很多应用。
应用程序可以在下面很多不同的项目

A project can have many applications.An application can below to many different project

您基本的多对多映射

目前这是我在我的功能NHibernate映射文件中设置

Currently this is what i have setup in my fluent nhibernate mapping files

 public class ProjectMap
 {
        HasMany(x => x.ProjectApplications)
      .AsBag().Inverse().Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(80);
 }

 public class ApplicationMap
 {
      HasMany(x => x.ProjectsApplications)
          .AsBag().Inverse().Fetch.Select().BatchSize(50);

 }

是否有任何缺点这是我看到有一个 HasManyToMany 语法所以我不知道,如果它使在生成或性能的查询等。

Is there any downside to this as i see there is a HasManyToMany syntax so I am not sure if it makes a difference in terms of the Queries that are generated or performance, etc

请指教

推荐答案

在一般有两种方法,因为你已经正确地提到:

In general there are two approaches, as you've correctly mentioned:


  • 配对对象的显式的映射,从而一对许多多到一

  • 不使用基础表的任何知识隐式映射多到许多

  • explicit mapping of the pairing object, resulting in one-to-many and many-to-one
  • implicit mapping without any knowledge of the underlying table using many-to-many

我的(我的个人陈述)的将避免多到许多在几乎所有情况下的(而在一些非常罕见的,真的可以用来管理对象的情况)的。

I (my personal statement) would avoid many-to-many in almost any scenario (while in some very rare, really admin object scenario could be used).

下面是我的一些尝试的,解释说:

Here are some of my tries, to explain that:





  • How to create NHibernate HasManyToMany relation
  • many-to-many with extra columns nhibernate
  • Nhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?

要添加更多的在这里,我首先会提到,与多到许多我们正在失去从模型中配对对象。永远。所以,一旦我们的客户会来问:请让我的关系之一主要或引入排序 - 我们根本不可能。的关系是因为它是。没办法如何扩展它。

To add more here, I would firstly mention, that with many-to-many we are loosing the pairing object from the model. Forever. So, once our customer will come and ask: please, make one of my relations Main, or introduce the Sorting - we simply cannot. The relation is as it is. No way how to extend it.

其次,而最有可能的 - 的很可能的:我们的客户会来问:你能为我创建一个过滤器,只选择项目这是有关应用有一些设置设为的真正的和...

And secondly, and most likely - very likely: our customer will come and ask: Could you create a filter for me, selecting only Projects which are related to Application having some setting set to true AND ...

这将是有点挑战性的多对许多情况。

有明确的配对对象中的场景带来与该第三个实体更多的开销。但可以转换成子查询

The scenario with explicit pairing object brings more overhead with that third Entity. But could be converted into Subqueries

有在子查询电源的一些例子:




  • Query on HasMany reference
  • Is it possible to query all objects that have one or more possible children using NHibernate?

嗯,这是我的观点。不是说这是正确的。但我的经验表明,与明确的对对象映射我们已经准备好扩展以及对复杂的查询。

Well, that is my point of view. Not saying it is correct. But my experience shows, that with explicit pair object mapping we are ready for extensions as well as for complex queries.

这篇关于我做多对多使用不当连贯NHibernate是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:24