"JPA Criteria Builder"按“求和"列的顺序 [英] JPA Criteria Builder order by a sum column

查看:658
本文介绍了"JPA Criteria Builder"按“求和"列的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下SQL查询.

select colA, sum(colB) as colB from tableA group by colA order by colB desc;

我已经使用jpa条件构建器来动态构建查询.

I have used jpa criteria builder to build the query dynamically.

List<String> selectCols = new ArrayList<>();
selectCols.add("colA");
List<String> sumColumns = new ArrayList<>();
sumColumns.add("colB");
List<String> groupByColumns = new ArrayList<>();
groupByColumns.add("colA");
List<String> orderingColumns = new ArrayList<>();
orderingColumns.add("colB");

CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> cQuery = builder.createQuery(Tuple.class);
Root<T> root = cQuery.from(getDomainClass());

List<Selection<?>> selections = new ArrayList<>();
for (String column : selectCols) {
    selections.add(root.get(column).alias(column));
}

if (sumColumns != null) {
    for (String sumColumn : sumColumns) {
        selections.add(builder.sum(root.get(sumColumn)).alias(sumColumn));
    }
}

cQuery.multiselect(selections);

List<Expression<?>> groupByExpressions = new LinkedList<>();

for (String column : groupByColumns) {
    groupByExpressions.add(root.get(column));
}

cQuery.groupBy(groupByExpressions);

List<Order> orders = new ArrayList<>();

for (String orderCol : orderingColumns) {

    orders.add(builder.desc(root.get(orderCol)));

}

cQuery.orderBy(orders);

entityManager.createQuery(cQuery).getResultList();

我调试并检查了正在执行的确切sql语句.

I debugged and checked the exact sql statement that is getting executed.

select colA as col_1_0, sum(colB) as col_2_0 from tableA group by colA order by colB desc;

正在执行的结果sql语句没有colB的别名作为colB.我正在使用MySql.出乎意料的是,当sql模式设置为NO_ENGINE_SUBSTITUTION时,该语句将被执行,并且得到正确的结果.但是,当sql模式为ONLY_FULL_GROUP_BY时,​​数据库将引发错误提示

The resultant sql statement that is getting executed doesn't have the alias for colB as colB. I am using MySql. Surprisingly, this statement gets executed and I get proper results when sql mode is set to NO_ENGINE_SUBSTITUTION. But when the sql mode is ONLY_FULL_GROUP_BY, the database throws an error saying

ORDER BY子句的表达式#1不在GROUP BY子句中,并且 包含非聚合列'colB',该列在功能上不起作用 取决于GROUP BY子句中的列;这与 sql_mode = only_full_group_by

Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'colB' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

我知道,在这种模式下,sql会验证查询,而我的查询将失败.如何使用jpa条件构建器创建别名,以便实际执行的语句包含sum列的适当别名?

I understand that in this mode, sql validates the query and my query fails. How do I create an alias with jpa criteria builder so that the actual statement getting executed contains a proper alias for the sum column?

注意:我使用spring-data-jpa,这是一个自定义的仓库实现.我可以使用@Query注释手动编写查询,但是查询的性质在group by,order by,where子句和select列上是动态的.因此,需要动态的标准构建器实现.

Note: I use spring-data-jpa and this is a custom implementation of repository. I can manually write the query using @Query annotation but the nature of the queries are dynamic on group by, order by, where clauses and the select columns. Hence the need for a dynamic criteria builder implementation.

推荐答案

尝试对汇总进行排序.以我的经验,列别名通常不能在查询本身中引用.

Try sorting over the aggregate. In my experience, column aliases usually can't be referenced in the query itself.

orders.add(builder.desc(builder.sum(sumColumn)));

这篇关于"JPA Criteria Builder"按“求和"列的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆