我需要创建一个查询,我需要COUNT(*)
and HAVING COUNT(*) = x
。
我正在使用一个使用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
Hibernate 类的代码:
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
,查询工作,但我没有得到HAVING COUNT(*)
SQL 中的过滤器......
基本上,查询尝试在主-详细信息模式中检索同时存在详细信息列表的所有主记录,例如,如果您想知道“哪些发票同时具有产品 A 和 B”。
这就是为什么如果我在IN
子句中有 3 个项目,我需要使用HAVING COUNT = 3
子句。
有什么想法或建议吗?此致,