NHibernate Fetch/FetchMany结果集中有很多重复项,如何使用ToFuture()进行修复 [英] NHibernate Fetch/FetchMany duplication in resultset, how to fix with ToFuture()

查看:99
本文介绍了NHibernate Fetch/FetchMany结果集中有很多重复项,如何使用ToFuture()进行修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用NHibernate,但遇到了一个缺点,我似乎无法解决自己的问题.我有一个对象树,希望在一次往返中从数据库中检索到它,但最终得到的是笛卡尔积.

我要检索的对象称为'AccountGroup''Concern''Advertiser''Product',我只希望获得活动用户具有权限的那些对象.

我的初始查询如下:

using (var session = OpenSession())
{
    return session.Query<AccountGroupEntity>()
        .FetchMany(a => a.Planners)
        .Where(a => a.Planners.Any(p => p.Id == userId))
        .FetchMany(a => a.Concerns)
        .ThenFetchMany(c => c.Advertisers)
        .ThenFetch(a => a.Products)
        .ToList();
}

这将不起作用,因为它将返回笛卡尔积,并且生成的实体将包含许多重复项.

但是,我不知道如何解决此问题.我已经看到了ToFuture()方法,该方法将允许我在同一往返中执行多个查询,但是我不知道如何以正确填充所有子集合的方式配置ToFuture()查询.

有人可以启发我如何使用ToFuture在单个查询中获取没有重复的整个树吗?

解决方案

我确实对此主题(我确实使用的解决方案)有一​​个答案.但是最后它的意思是不要使用Fetch"-进行不同的处理.因此,请至少将其作为建议.

检查此问题与解答;答:

如何在不进行NHibernate重复的情况下急切地进行负载关联?

小引用:

获取集合是一项困难的操作.它有很多副作用(如您所知,当获取更多集合时).但是即使获取了一个集合,我们仍在加载许多重复的行.

换句话说,获取是一个脆弱的功能,应该在很少的情况下明智地使用.那该怎么用呢?该怎么解决?

通过内置的NHibernate功能获利:

19.1.5.使用批量提取

NHibernate可以有效地使用批处理获取,也就是说,如果访问了一个代理(或集合),NHibernate可以加载多个未初始化的代理.批处理获取是对惰性选择获取策略的优化.有两种方法可以调整批处理获取:在类和集合级别.

批量提取类/实体更容易理解.假设您在运行时遇到以下情况:在ISession中加载了25个Cat实例,每个Cat都有对其所有者Person的引用. Person类与代理lazy ="true"映射.如果您现在遍历所有cat并在每个cat上调用cat.Owner,则NHibernate默认情况下将执行25条SELECT语句,以检索代理的所有者.您可以通过在Person映射中指定一个批处理大小来调整此行为:

<class name="Person" batch-size="10">...</class>

NHibernate现在将仅执行三个查询,模式为10、10、5.

您还可以启用集合的批量提取.例如,如果每个人都有一个懒惰的Cat集合,并且当前在ISesssion中加载了10个人,则对所有人进行迭代将生成10个SELECT,每个对person.Cat的调用都会选择一个.如果您在Person的映射中为Cats集合启用了批量提取,则NHibernate可以预提取集合:

<class name="Person">
    <set name="Cats" batch-size="3">
        ...
    </set>

根据我的经验,这种方法是无条件的.适用于我们的设置是batch-size="25".

如果您要求任何类型的实体(via session.Get() or .QueryOver()...)-在会话打开之前,我们第一次接触相关的引用或集合-会分几批加载...否1 + N SELECT问题...

摘要:标记所有的类,并使用batch-size="x" (x可能是25)收集所有集合.这将支持对根实体的干净查询-在打开会话之前,所有相关内容都将在少数SELECTS中加载. x可以调整,有些可能会更高.

I'm relatively new to using NHibernate and I'm running into a shortcoming I can't seem to work myself around. I have an object tree that I wish to retrieve from the database in a single roundtrip but end up with a cartesian product.

The objects I'm trying to retrieve are called 'AccountGroup', 'Concern', 'Advertiser' and 'Product' and I only wish to get those objects where the active user has permissions for.

My initial query looked like this:

using (var session = OpenSession())
{
    return session.Query<AccountGroupEntity>()
        .FetchMany(a => a.Planners)
        .Where(a => a.Planners.Any(p => p.Id == userId))
        .FetchMany(a => a.Concerns)
        .ThenFetchMany(c => c.Advertisers)
        .ThenFetch(a => a.Products)
        .ToList();
}

This won't work as it will return a cartesian product and the resulting entities will contain many duplicates.

However, I have NO idea how to fix this. I've seen the ToFuture() method that will allow me to execute more than one query in the same roundtrip, but I have no clue how to configure my ToFuture() query in such a way that it populates all the child collections properly.

Could anyone shine some light on how I can use ToFuture to fetch the entire tree in a single query without duplicates?

解决方案

I do have an answer to this topic, solution which I do use. But it at the end means "do not use Fetch" - do it differently. So, please, take it at least as a suggestion.

Check this Q & A:

How to Eager Load Associations without duplication in NHibernate?

Small cite:

Fetching Collections is a difficult operation. It has many side effects (as you realized, when there are fetched more collections). But even with fetching one collection, we are loading many duplicated rows.

Other words, Fetching is a fragil feature, and should be used wisely in very few scenarios, I'd say. So what to use? How to solve that?

Profit from a built in NHibernate feature:

19.1.5. Using batch fetching

NHibernate can make efficient use of batch fetching, that is, NHibernate can load several uninitialized proxies if one proxy is accessed (or collections. Batch fetching is an optimization of the lazy select fetching strategy. There are two ways you can tune batch fetching: on the class and the collection level.

Batch fetching for classes/entities is easier to understand. Imagine you have the following situation at runtime: You have 25 Cat instances loaded in an ISession, each Cat has a reference to its Owner, a Person. The Person class is mapped with a proxy, lazy="true". If you now iterate through all cats and call cat.Owner on each, NHibernate will by default execute 25 SELECT statements, to retrieve the proxied owners. You can tune this behavior by specifying a batch-size in the mapping of Person:

<class name="Person" batch-size="10">...</class>

NHibernate will now execute only three queries, the pattern is 10, 10, 5.

You may also enable batch fetching of collections. For example, if each Person has a lazy collection of Cats, and 10 persons are currently loaded in the ISesssion, iterating through all persons will generate 10 SELECTs, one for every call to person.Cats. If you enable batch fetching for the Cats collection in the mapping of Person, NHibernate can pre-fetch collections:

<class name="Person">
    <set name="Cats" batch-size="3">
        ...
    </set>

My experience, this approach is pricless. The setting working for us is batch-size="25".

If you ask for any kind of Entity (via session.Get() or .QueryOver()...) - until session is open, the first time we touch related reference or collection - it is loaded in few batches... No 1 + N SELECT Issue...

Summary: Mark all your classes, and all collection with batch-size="x" (x could be 25). That will support clean queries over root Entities - until session is open, all related stuff is loaded in few SELECTS. The x could be adjusted, for some could be much more higher...

这篇关于NHibernate Fetch/FetchMany结果集中有很多重复项,如何使用ToFuture()进行修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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