クライテリア API で構築されたクエリによって返される結果をカウントしたいと考えています。これらのクエリを書き直したくありません。私は試した
(Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
これは機能する可能性がありますが、Hibernate 3.2.4 が生成する結合と制限によっては
無効な sql (DB2: com.ibm.db2.jcc.b.co: DB2 SQL エラー: SQLCODE=-119、SQLSTATE=42803、これは「HAVING 句の列または式が無効です」を意味します)。以下の例...
元のSQLが
select * from ... where ... order by ...
count(*)
次に、カウント射影は基本的に*を次のように置き換えるだけです
select count(*) as y0_ from ... where ... order by ...
エラーが発生する可能性があります。これは、元の SQL をラップして先頭に追加することで簡単に修正できますselect count(*)
。
select count(*) from (select * from ... where ... order by ...);
これをどのように達成できますか、または他に何を提案しますか?
無効な sql の例を次に示します。
select count(*) as y0_
from UZI.Ereignis this_
left outer join UZI.Zaehlerzustand zaehlerzus1_ on this_.zustandHz_id=zaehlerzus1_.id
left outer join UZI.Mandant mandantali3_ on zaehlerzus1_.mandant_id=mandantali3_.id
left outer join UZI.Zaehlerzustand zaehlerzus2_ on this_.zustandNz_id=zaehlerzus2_.id
left outer join UZI.Mandant mandantali4_ on zaehlerzus2_.mandant_id=mandantali4_.id
where (zaehlerzus1_.zaehlernummer like ? or zaehlerzus2_.zaehlernummer like ?)
and (mandantali3_.id=? or mandantali4_.id=?)
order by this_.id desc