Hibernate only error: "Column must appear in the GROUP BY clause or be used in an aggregate function"

Hibernate only error: "Column must appear in the GROUP BY clause or be used in an aggregate function"

我有这个由 hibernate 生成的查询(打印在控制台上),如果直接执行 throwg pgAdmin4:

它工作正常
select
        my_entity0_.status as col_0_0_,
        calc_sub_status(
            my_entity0_.status,
            my_entity0_.sub_status
        ) as col_1_0_,
        count(my_entity0_.id) as col_2_0_ 
    from
        demand my_entity0_
    group by
        my_entity0_.status ,
        calc_sub_status(
            my_entity0_.status,
            my_entity0_.sub_status
        )

但是在我的应用程序中(使用 Spring 引导和休眠)我遇到了这个异常:

SQL Error: 0, SQLState: 42803
ERROR: column "my_entity0_.sub_status" must appear in the GROUP BY clause or be used in an aggregate function

如果相同的查询在 pgAdmin 中有效,而不是通过 jdbc 可以做什么?

PS:calc_sub_status是一个自定义函数,接收2个字符串和returns一个字符串。

PS2:标准代码:

cq.multiselect(status, calcSubStatus, cb.count(root))
                .groupBy(status, calcSubStatus);

我能够跳过这个仅限休眠的错误,将查询更改为:

select
        my_entity0_.status as col_0_0_,
        calc_sub_status(
            my_entity0_.status,
            my_entity0_.sub_status
        ) as col_1_0_,
        count(my_entity0_.id) as col_2_0_ 
    from
        demand my_entity0_
    group by
        my_entity0_.status ,
        2

区别在于按子句分组,我按结果的位置“2”分组,而不是重复函数调用。

在 pgAdmin 中两种方法都有效,但在休眠中只有这种方法有效。

实现这个的条件函数是cb.literal({position}):

cq.multiselect(status, calcSubStatus, cb.count(root))
                .groupBy(status, cb.literal(2));