Nic*_*400 10 hibernate having-clause
我需要创建一个查询,我需要COUNT(*)
和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
Run Code Online (Sandbox Code Playgroud)
这是我使用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() )
);
Run Code Online (Sandbox Code Playgroud)
错误是:
!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)
Run Code Online (Sandbox Code Playgroud)
如果我用CustomProjections
类注释该行,查询工作,但我没有得到HAVING COUNT(*)
SQL中的过滤器...
基本上,查询尝试在主 - 详细信息模式中检索同时存在详细信息列表的所有主记录,例如,如果您希望知道"哪些发票同时具有产品A和B".
这就是为什么如果我在IN
条款中有3个项目,我需要使用HAVING COUNT = 3
子句.
有什么想法或建议吗?最好的祝福,
Nic*_*400 10
我解决了这个问题.我用以下代码替换CusotmProjections类:
.add( Projections.sqlGroupProjection("ensayo_id", groupBy , alias, types));
Run Code Online (Sandbox Code Playgroud)
其中groupBy,别名和类型是:
String groupBy = "ensayo_id" + " having " + "count(*) = " + String.valueOf(lineas.size());
String[] alias = new String[1];
Alias[0] = "ensayo_id";
Type[] types = new Type[1];
types[0] = Hibernate.INTEGER;
Run Code Online (Sandbox Code Playgroud)
神奇的是在groupby String上. -
归档时间: |
|
查看次数: |
12977 次 |
最近记录: |