2011-07-21 77 views
0

帶有Restrictions.Discjunction()或Restrictions.Or()的投影將產生WHERE子句而不是HAVING子句。這將導致錯誤(WHERE子句不能引用聚合表達式)。帶限制的投影。交換接口

例子:

.CreateCriteria(typeof(SalesOrderLine), "SOL") 
.CreateCriteria("DeliveryOrderLines", "DOL", JoinType.LeftOuterJoin) 
.SetProjection(
    Projections.GroupProperty("SOL.Key").As("SalesOrderLineId"), 
    Projections.GroupProperty("SOL.Item"), 
    Projections.GroupProperty("SOL.Description"), 
    Projections.GroupProperty("SOL.UnitPrice"), 
    Projections.GroupProperty("SOL.Quantity"), 
    Projections.GroupProperty("SOL.DiscountPercentage"), 
    Projections.GroupProperty("SOL.SalesOrder")) 
.Add(Restrictions.Or(
    Restrictions.IsNull(Projections.Sum("DOL.Quantity")), 
    Restrictions.GtProperty("SOL.Quantity", Projections.Sum("DOL.Quantity")))), 
.List(); 

SQL結果:

SELECT this_.SalesOrderLineId as y0_, this_.Item as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Quantity as y4_, this_.DiscountPercentage as y5_, this_.SalesOrderId as y6_ FROM SalesOrderLine this_ left outer join DeliveryOrderLine dol1_ on this_.SalesOrderLineId=dol1_.SalesOrderLineId 
WHERE (sum(dol1_.Quantity) is null or this_.Quantity > sum(dol1_.Quantity)) 
GROUP BY this_.SalesOrderLineId, this_.Item, this_.Description, this_.UnitPrice, this_.Quantity, this_.DiscountPercentage, this_.SalesOrderId 

難道我做錯了?或者這是Nhibernate 3.1中的一個錯誤?

在此先感謝:)。

回答

1

上次我檢查了HAVING子句不被Criteria API支持。您需要使用支持HAVING的HQL。

要使用標準的API使用,你就必須修改您的查詢到這一點:

SELECT this_.SalesOrderLineId as y0_, this_.Item as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Quantity as y4_, this_.DiscountPercentage as y5_, this_.SalesOrderId as y6_ FROM SalesOrderLine this_ left outer join DeliveryOrderLine dol1_ on this_.SalesOrderLineId=dol1_.SalesOrderLineId 
WHERE (select sum(Quantity) from DeliveryOrderLine) is null or (select sum(Quantity) from > SalesOrderLine) > (select sum(Quantity) from DeliveryOrderLine) 
GROUP BY this_.SalesOrderLineId, this_.Item, this_.Description, this_.UnitPrice, this_.Quantity, this_.DiscountPercentage, this_.SalesOrderId 

,我已經給會因爲多次和計算的一些性能問題的解決方案,但它會得到工作完成。

你也可以試試這個,但我不知道這是否會正常運行:

SELECT this_.SalesOrderLineId as y0_, this_.Item as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Quantity as y4_, this_.DiscountPercentage as y5_, this_.SalesOrderId as y6_, SUM(dol1_Quantity) as sum1, SUM(this_.Quantity) as sum2 from SalesOrderLine this_ left outer join DeliveryOrderLine dol1_ on this_.SalesOrderLineId=dol1_.SalesOrderLineId 
WHERE sum1 is null or sum1 > sum2 
GROUP BY this_.SalesOrderLineId, this_.Item, this_.Description, this_.UnitPrice, this_.Quantity, this_.DiscountPercentage, this_.SalesOrderId 

希望這有助於!

+0

謝謝varun! 性能在這裏很重要,所以我不能使用你的第一個標準API解決方案。 你能指導我解決你的第二個問題嗎? 我嘗試給我的預測別名,但我不能用它的限制。 –