我需要創建一個查詢,我需要COUNT(*)
和HAVING COUNT(*) = x
。如何使用HAVING COUNT(*)與休眠
我正在使用的工作,使用CustomProjection
類,我下載的地方。
這是我嘗試實現SQL:
select count(*) as y0_, this_.ensayo_id as y1_ from Repeticiones this_
inner join Lineas linea1_ on this_.linea_id=linea1_.id
where this_.pesoKGHA>0.0 and this_.nroRepeticion=1 and linea1_.id in (18,24)
group by this_.ensayo_id
having count(*) = 2
這是代碼,我在那裏用Projection
休眠類:
critRepeticion.setProjection(Projections.projectionList()
.add(Projections.groupProperty("ensayo"))
.add(CustomProjections.groupByHaving("ensayo_id",Hibernate.LONG,"COUNT(ensayo_id) = "+String.valueOf(lineas.size()))
.add(Projections.rowCount())
);
的錯誤是:
!STACK 0
java.lang.NullPointerException
at org.hibernate.criterion.ProjectionList.toSqlString(ProjectionList.java:50)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getSelect(CriteriaQueryTranslator.java:310)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:71)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at ar.com.cse.cseagro.controller.RepeticionController.buscarEnsayo(RepeticionController.java:101)
如果我用CustomProjections
類評論該行,查詢工作,但我沒有得到t他HAVING COUNT(*)
過濾器中的SQL ...
基本上,查詢嘗試檢索,在主 - 細節架構,所有主記錄的細節列表同時存在,如果你想知道「哪些發票有兩種產品,A和B「。
那爲什麼如果我在IN
子句中有3個項目,我需要使用HAVING COUNT = 3
子句。
任何想法或建議? 此致敬禮,
對不起,我想通了這個問題。我將CusotmProjections類替換爲:.add(Projections。sqlGroupProjection(「ensayo_id」,groupBy,別名,類型)),其中groupBy,別名和類型是:\t \t \t String groupBy =「ensayo_id」+「having」+「count(*)=」+ String.valueOf(lineas。尺寸()); \t \t \t String [] alias = new String [1]; \t \t \t alias [0] =「ensayo_id」; \t \t \t Type [] types = new Type [1]; \t \t \t types [0] = Hibernate.INTEGER;魔術在groupby String上。 – Nicolas400 2011-12-22 15:33:15