2010-09-16 61 views
0

我有以下查询HNibernate:有助于避免N + 1查询

DetachedCriteria criteria = DetachedCriteria.For(typeof(Income)) 
       .CreateAlias("Product", "p") 
       .SetProjection(
        Projections.ProjectionList() 
         .Add(Projections.GroupProperty("Product")) 
         .Add(Projections.Sum("Quantity")) 
       ); 

其转换为SQL:

SELECT this_.Product_id as y0_, 
     sum(this_.Quantity) as y1_ 
FROM  income this_ 
     inner join products p1_ 
      on this_.Product_id = p1_.Id 
GROUP BY this_.Product_id 

我的域名:

class Product 
{ 
    IList<Income> Incomes {get;set;} 
} 

class Income 
{ 
    Product Product {get;set;} 
} 

迭代时返回的集合我对每个Product实体都有一个查询。 如何在一个查询中获取所有集合?

+0

你可以尝试使用SetFetchMode属性吗? http://nhprof.com/Learn/Alerts/SelectNPlusOne – rebelliard 2010-09-16 13:01:30

+0

已经尝试过 - SetFetchMode(“p”,FetchMode.Join)没有效果 – kilonet 2010-09-16 13:18:33

回答

0

我喜欢HQL该类型的查询......

select i.Product, sum(i.Quantity) 
from Income i 
group by /*all product properties*/ 
0

你使用Enumerable而不是List执行查询?