我想加载根实体,并立即加载它的所有子集合和聚合成员。
我一直在尝试在FluentNHibernate中使用SetFetchMode,但我在一个子集合中得到了重复,因为我有3个级别的深度。不幸的是,DistinctRootEntityResultTransformer只删除了根目录的重复。
return Session.CreateInvoiceBaseCriteria(query, archived)
.AddOrder(new Order(query.Order, query.OrderType == OrderType.ASC))
.SetFetchMode("States", FetchMode.Eager)
.SetFetchMode("Attestations", FetchMode.Eager)
.SetFetchMode("AttestationRequests", FetchMode.Eager)
.SetFetchMode("AttestationRequests.Reminders", FetchMode.Eager)
.SetResultTransformer(new DistinctRootEntityResultTransformer())
.List<Invoice>();我可以使用多个查询或类似的东西来归档吗?
此外,这种方法不会从数据库中产生不必要的巨大结果集吗?
有什么建议吗?
发布于 2009-06-04 23:00:33
找到了一个解决方案,但它并不美观。首先,我找到所有的发票ID,然后在多查询中使用它们,最后通过HashedSet过滤结果。由于大量的项目,有时我不能使用普通的Restriction.In,被迫将其作为字符串发送。
有什么建议的调整吗?
var criteria = Session.CreateInvoiceBaseCriteria(query, archived)
.SetProjection(Projections.Id());
var invoiceIds = criteria.List<int>();
if (invoiceIds.Count > 0)
{
var joinedIds = JoinIDs(criteria.List<int>()); // To many ids to send them as parameters.
var sql1 = string.Format("from Invoice i inner join fetch i.States where i.InvoiceID in ({0}) order by i.{1} {2}", joinedIds, query.Order, query.OrderType.ToString());
var sql2 = string.Format("from Invoice i inner join fetch i.AttestationRequests where i.InvoiceID in ({0})", joinedIds);
var sql3 = string.Format("from Invoice i inner join fetch i.Attestations where i.InvoiceID in ({0})", joinedIds);
var invoiceQuery = Session.CreateMultiQuery()
.Add(sql1)
.Add(sql2)
.Add(sql3);
var result = invoiceQuery.List()[0];
return new UniqueFilter<Invoice>((ICollection)result);
}
return new List<Invoice>();发布于 2009-10-21 16:57:37
回答你的问题:是的,它会产生巨大的结果集。
我建议:
发布于 2009-06-02 08:39:19
虽然这可能不是你想要的,但我建议你看看这篇文章:
Eager loading aggregate with many child collections
如果你浏览一下这个站点的其余部分,你会发现更多的文章讨论了快速加载和其他很棒的nHibernate内容。
https://stackoverflow.com/questions/937388
复制相似问题