简体   繁体   English

在QueryOver中

[英]Having in QueryOver

we are struggling with the following problem.. ORM solution of our choice is NHibernate and we want to write queries using QueryOver style.. Right now there is a new puzzle to solve, we want to make a query like: 我们正在努力解决以下问题..我们选择的ORM解决方案是NHibernate,我们想使用QueryOver样式编写查询。现在有一个新的难题要解决,我们想做一个查询,如:

select sp.Id, SUM(p.PriceAmount), SUM(i.BruttoAmount)  from SellerProfile sp
left join SellerProfile_Invoice spi on spi.SellerProfile = sp.Id
left join Invoice i on spi.Invoice = i.Id
left join SellerProfile_Payment spp on spp.SellerProfile = sp.Id
left join Payment p on spp.Payment = p.Id
where i.PaymentDate < '2011-07-12'
group by sp.Id
having SUM(ISNULL(p.PriceAmount,0)) - SUM(ISNULL(i.BruttoAmount,0)) < 0

So we've wrote code like this: 所以我们编写了这样的代码:

Invoice invoice = null;
Payment payment = null;
SellerProfile seller = null;

var sellerIds = Session.QueryOver<SellerProfile>(() => seller)
                .Left.JoinQueryOver(() => seller.Payments, () => payment)
                .Left.JoinQueryOver(() => seller.Invoices, () => invoice)
                .Where(() => invoice.PaymentDate < DateTime.Now - timeSpan)
                .Select(Projections.Group(() => seller.Id))
                .Where(Restrictions.Lt(new ArithmeticOperatorProjection("-", NHibernateUtil.Decimal, Projections.Sum(() => payment.Price.Amount), Projections.Sum(() => invoice.Brutto.Amount)), 0)).List<int>();

Generated SQL looks like this: 生成的SQL如下所示:

SELECT this_.Id as y0_ 
FROM SellerProfile this_ inner join ResourceOwner this_1_ on this_.Id=this_1_.Id 
inner join Resource this_2_ on this_.Id=this_2_.Id
left outer join SellerProfile_Payment payments4_ on this_.Id=payments4_.SellerProfile
left outer join Payment payment2_ on payments4_.Payment=payment2_.Id
left outer join SellerProfile_Invoice invoices6_ on this_.Id=invoices6_.SellerProfile
left outer join Invoice invoice1_ on invoices6_.Invoice=invoice1_.Id
WHERE invoice1_.PaymentDate < @p0
and (sum(payment2_.PriceAmount) - sum(invoice1_.BruttoAmount)) < @p1
GROUP BY this_.Id

But it's throwing an exception, because it puts and clause to first where instead of having in the last line and our SQL does not work... 但它抛出一个异常,因为它将and条款先where ,而不是having在最后一行,我们的SQL不工作...

Any help? 有帮助吗? Thanks... 谢谢...

AFAIK the QueryOver, LINQ, and Criteria API does not support HAVING clause logic (NH 3.1) AFAIK QueryOver,LINQ和Criteria API不支持HAVING子句逻辑(NH 3.1)

You can use HQL though. 你可以使用HQL。

HQL Examples HQL示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM