问题描述
我有一个复杂的实体,其中包含许多子对象,这些对象也很复杂:
I've got complex entity with a lot of children collection of objects, which are complex too:
public class Order : AdvancedBaseOrder, ICheckable
{
public virtual ICollection<RouteUnit> RouteUnits
public virtual ICollection<Invoice> Invoices
public virtual ICollection<Call> Calls;
public virtual ICollection<Payment> Payments;
......
}
我的付款类别还汇总了许多其他对象
My payment class aggregates a lot of other objects
public class Payment: ICheckable
{
public virtual A A;
public virtual B B;
public virtual C C;
public virtual D D;
......
}
我想通过2个查询获得订单:
I want get order with 2 queries:
- 无需付款即可加载订单实体(FetchMode.Lazy)
- 通过加入其对象的订单加载付款
- 合并\合并订单及其付款
我不希望nhib进行延迟加载付款,因为我想覆盖付款对象的提取策略.
I don't want lazy load payments by nhib, cause I would like override fetch strategies for payment's objects.
所以我的问题是如何将两个查询结果合并为一个聚合.谢谢
So my question is how can i merge combine two result of queries in one aggregate. Thanks
推荐答案
在这种情况下,batch-size="25"
设置可以为您完成这项工作.在文档中了解更多信息: 19.1.5.使用批量提取.
In this case, batch-size="25"
setting could do the job for you. Read more in the documentation: 19.1.5. Using batch fetching.
批处理大小可以应用于 class 或 collection 映射:
batch size coulde be applied on a class or collection maping:
付款类别
<class name="Payment" batch-size="10">...</class>
收款
<class name="Order">
<set name="Payments" batch-size="3">
...
</set>
</class>
简而言之,批处理的工作方式:NHibernates加载所有Orders
的集合.然后根据批量大小设置(例如25)创建对由刚刚加载的 Orders 的ID
过滤的Payments
的少量调用:
How the batching works in a nutshell: NHibernates loads the set of all Orders
. Then based on a batch-size setting (e.g. 25) creates few calls to Payments
filtered by ID
s of just loaded Orders:
合并将在NHibernate会话中为您完成.根据我的经验,这是最强大的映射.批处理.
The merge will be done for you inside NHibernate session. From my experience this is most powerful mapping... Lazy & Batching.
这篇关于NHibernate.用2个查询加载实体并合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!