如何在休眠中使用 HAVING COUNT(*)

发布于 2024-12-22 06:55:59 字数 1791 浏览 2 评论 0原文

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

这是代码,我在其中使用 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 类注释该行,查询工作,但我没有在 SQL 中得到 HAVING COUNT(*) 过滤器...

基本上,查询尝试在主从模式中检索所有主记录,其中详细信息列表同时存在,就像您想知道的那样“哪些发票同时包含产品 A 和 B”。

这就是为什么如果我在 IN 子句中有 3 个项目,我需要使用 HAVING COUNT = 3 子句。

有什么想法或建议吗? 此致,

I need to create a query and I need COUNT(*) and HAVING COUNT(*) = x.

I'm using a work around that uses the CustomProjection class, that I downloaded somewhere.

This is the SQL that I try to achieve:

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

This is the code, where I use the Projection Hibernate class:

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() )
                );

The error is:

!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)

If I comment the line with CustomProjections class, the query work, but I don't get the HAVING COUNT(*) filter in the SQL ...

Basically the query try to retrieve, in a master - detail schema, all the master records where a list of details are simultaneously present, like if you want tho know "which invoices have both products, A and B".

That why if I got 3 items in the IN clause, I need to use HAVING COUNT = 3 clause.

Any idea or suggestion?
Best regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠佳期 2024-12-29 06:55:59

我发现了问题所在。我将 CusotmProjections 类替换为:

.add( Projections.sqlGroupProjection("ensayo_id", groupBy , alias, types));

其中 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;

神奇之处在于 groupby 字符串。 –

I figured out the problem. I replace CusotmProjections class, with:

.add( Projections.sqlGroupProjection("ensayo_id", groupBy , alias, types));

where groupBy, alias and types are:

 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;

and the magic is on groupby String. –

三生池水覆流年 2024-12-29 06:55:59

如果有人需要在 grails 中执行此操作,则类似于:

projections {
    groupProperty("id")
    sqlGroupProjection(...)
    rowCount()
}

sqlGroupProjection 自 2.2.0 起可用

/**
 * Adds a sql projection to the criteria
 * 
 * @param sql SQL projecting
 * @param groupBy group by clause
 * @param columnAliases List of column aliases for the projected values
 * @param types List of types for the projected values
 */
protected void sqlGroupProjection(String sql, String groupBy, List<String> columnAliases, List<Type> types) {
    projectionList.add(Projections.sqlGroupProjection(sql, groupBy, columnAliases.toArray(new String[columnAliases.size()]), types.toArray(new Type[types.size()])));
}

http://grepcode.com/file/repo1.maven.org/maven2/org.grails/grails-hibernate/2.2.0/grails/orm/HibernateCriteriaBuilder.java/#267

If someone needs to do it in grails it would be like:

projections {
    groupProperty("id")
    sqlGroupProjection(...)
    rowCount()
}

Where sqlGroupProjection is available since 2.2.0

/**
 * Adds a sql projection to the criteria
 * 
 * @param sql SQL projecting
 * @param groupBy group by clause
 * @param columnAliases List of column aliases for the projected values
 * @param types List of types for the projected values
 */
protected void sqlGroupProjection(String sql, String groupBy, List<String> columnAliases, List<Type> types) {
    projectionList.add(Projections.sqlGroupProjection(sql, groupBy, columnAliases.toArray(new String[columnAliases.size()]), types.toArray(new Type[types.size()])));
}

http://grepcode.com/file/repo1.maven.org/maven2/org.grails/grails-hibernate/2.2.0/grails/orm/HibernateCriteriaBuilder.java/#267

明天过后 2024-12-29 06:55:59

这是我的示例,它工作正常,也许有用:

我的 sql 查询:

从 MY_TABLE 分组中选择 COLUMN1、sum(COLUMN2)
COLUMN1 的 sum(COLUMN2) > 1000;

标准是:

 Criteria criteria = getCurrentSession().createCriteria(MyTable.Class);
  ProjectionList projectionList = Projections.projectionList();
  projectionList.add(Projections.property("column1"), "column1");
  projectionList.add(Projections.sqlGroupProjection("sum(column2)  sumColumn2 ", "COLUMN1 having sum(COLUMN2) > 1000" , new String[]{"sumColumn2"}, new org.hibernate.type.Type[]{StandardBasicTypes.STRING}));
  criteria.setProjection(projectionList);
  criteria.List();

Here is my sample, it works fine, maybe useful :

My sql query :

select COLUMN1, sum(COLUMN2) from MY_TABLE group by
COLUMN1 having sum(COLUMN2) > 1000;

And Criteria would be :

 Criteria criteria = getCurrentSession().createCriteria(MyTable.Class);
  ProjectionList projectionList = Projections.projectionList();
  projectionList.add(Projections.property("column1"), "column1");
  projectionList.add(Projections.sqlGroupProjection("sum(column2)  sumColumn2 ", "COLUMN1 having sum(COLUMN2) > 1000" , new String[]{"sumColumn2"}, new org.hibernate.type.Type[]{StandardBasicTypes.STRING}));
  criteria.setProjection(projectionList);
  criteria.List();
栖迟 2024-12-29 06:55:59

criteria.add(Restrictions.sqlRestriction("1=1 具有 count(*) = 2"));

criteria.add(Restrictions.sqlRestriction("1=1 having count(*) = 2"));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文