急于使用Criteria API在NHibernate中加载集合 [英] Load collections eagerly in NHibernate using Criteria API

查看:108
本文介绍了急于使用Criteria API在NHibernate中加载集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体A,其中有许多实体B和实体C.所有实体A,B和C都有一些引用x,y和z,应尽快加载.

I have an entity A which HasMany entities B and entities C. All entities A, B and C have some references x,y and z which should be loaded eagerly.

我想从数据库中读取所有实体A,并使用标准API急切地加载B和C的集合. 到目前为止,我已经能够急切地获取"A"中的引用.但是,在加载集合时,它们中的引用会被延迟加载.

I want to read from the database all entities A, and load the collections of B and C eagerly using criteria API. So far, I am able to fetch the references in 'A' eagerly. But when the collections are loaded, the references within them are lazily loaded.

这是我的方法

            AllEntities_A =
            _session.CreateCriteria(typeof(A))
            .SetFetchMode("x", FetchMode.Eager)
            .SetFetchMode("y", FetchMode.Eager)
            .List<A>().AsQueryable();

使用Fluent的实体A的映射如下所示. _B和_C是B&的私人ILists. C分别在A中.

The mapping of entity A using Fluent is as shown below. _B and _C are private ILists for B & C respectively in A.

        Id(c => c.SystemId);
        Version(c => c.Version);
        References(c => c.x).Cascade.All();
        References(c => c.y).Cascade.All();

        HasMany<B>(Reveal.Property<A>("_B"))
            .AsBag()                
            .Cascade.AllDeleteOrphan()
            .Not.LazyLoad()
            .Inverse()
            .Cache.ReadWrite().IncludeAll();
        HasMany<C>(Reveal.Property<A>("_C"))
            .AsBag()
            .Cascade.AllDeleteOrphan()
            .LazyLoad()
            .Inverse()
            .Cache.ReadWrite().IncludeAll();

我不想更改映射文件,并且希望尽快加载整个实体A.即我应该得到一个A的列表,其中将有B和C的列表,它们的参考属性也将被急切地加载

I don't want to make changes to the mapping file, and would like to load the entire entity A eagerly. i.e. I should get a List of A's where there will be List of B's and C's whose reference properties will also be loaded eagerly

推荐答案

您正在尝试在此处做笛卡尔乘积.我认为NHibernate要求将关系映射为集合而不是袋,因为袋允许重复.

You're trying to do a cartesian product here. I think NHibernate requires mapping the relations as sets instead of bags to do that, since bags allow duplicates.

无论如何,笛卡尔积是非常低效的.请改用多查询或以后的查询.

Anyway, cartesian products are very inefficient. Use a multi-query or future queries instead.

请参阅:

  • Nhibernate: eager loading two child collections (one being a component list)
  • https://nhibernate.jira.com/browse/NH-1471
  • http://nhibernate.info/blog/2008/09/06/eager-loading-aggregate-with-many-child-collections.html
  • http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-associations-efficiently-with-nhibernate.aspx

这篇关于急于使用Criteria API在NHibernate中加载集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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