NHibernate Criteria with Distinct Parent Load All Children?

只谈情不闲聊 提交于 2019-12-02 07:29:44
Radim Köhler

I would suggest to use the batch-size="" setting. It will end up in
1) one query issued by us (query.List<MessageQueue>();),
2) NHibernate will use then one (or just few) query/ies to load collection for each returned MessageQueue.

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>

The fluent alternative is (both collection and class level)

.BatchSize(25)

Also check:

NOTE: At the end, the number passed as a batch size, e.g. 25, seems to be used as its half - 12. So if you do paging on a size of 25, try to use SetBatchSize(50)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!