我需要创建一个查询,我需要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