2011-05-26 61 views
0

首先我知道GroubBy多個屬性尚未實現。Nhibernate GroupBy倍數和計數

我想要做的是

SELECT count(*) 
FROM ( 
    SELECT this_.SubmissionDate as y0_ 
    FROM [Coupon] this_ 
    WHERE this_.PaymentOrder_id is null 
    GROUP BY this_.SubmissionDate, 
      this_.Deal_id 
) AS query 

我在NHibernate的最好的是

Session.QueryOver<Coupon>().Select(Projections.Group<Coupon>(e => e.SubmissionDate),Projections.Group<Coupon>(e => e.Deal.Id)).Future<object[]>().Count() 

帶來整套,然後數了。

我發現this並創造了這個

var count = Session.CreateQuery("select count(*) from (select c.SubmissionDate from Coupon c where c.PaymentOrder.Id is null group by c.SubmissionDate, c.Deal.Id) as query").FutureValue<Int32>(); 

不工作。拋出一個

Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 21 

Exception Details: NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 21 

多個異常

[QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 21] 
    NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException() +118 
    NHibernate.Hql.Ast.ANTLR.HqlParseEngine.Parse() +416 
+0

會發生什麼它? – 2011-05-26 13:26:35

+0

編輯添加異常 – 2011-05-26 13:32:08

+0

Antlr異常似乎是由於不正確的HQL語法。 請參閱:http://stackoverflow.com/questions/3799087/nhibernate-antlr-runtime-noviablealtexception。 像上面的鏈接一樣,QuerySyntaxException是否包含異常? – Tomas 2011-05-26 14:07:34

回答

0

您是否嘗試過這樣的事情,會做計數的內存?

var count = Session.CreateQuery("select c.SubmissionDate from Coupon c where c.PaymentOrder.Id is null group by c.SubmissionDate, c.Deal.Id").ToList().Count; 

HQL似乎不支持外子查詢選擇和其中:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

否則,你可以創建一個存儲過程,並在你的映射添加:當您運行Correct NHibernate Mapping For Stored Procedure?

+0

不會返回整組結果並將它們計入內存中嗎? – 2011-05-26 15:34:32