Проекции с ограничениями. Дисконнект - PullRequest
0 голосов
/ 21 июля 2011

Проекции с 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 Ответ

1 голос
/ 21 июля 2011

В прошлый раз, когда я проверял, условие HAVING не поддерживается Criteria API.Вам нужно будет использовать HQL, который поддерживает HAVING.

Для использования с Criteria 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

Надеюсь, это помогло!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...