如何按 JPA/Hibernate 中的聚合列排序?

Zee*_*mee 2 java hibernate jpa

通过执行这个 JPQL:

select o.key, count(o.id), sum(o.errors) from MyEntity o
group by o.key
Run Code Online (Sandbox Code Playgroud)

Hibernate 提交以下 SQL:

select
    myentityn0_.key as col_0_0_,
    count(myentityn0_.id) as col_1_0_,
    sum(myentityn0_.errors) as col_2_0_
from
    MYENTITY myentityn0_ 
group by
    myentityn0_.key
Run Code Online (Sandbox Code Playgroud)

但是如何在不使用 Hibernate 创建的列名的情况下在这种情况下使用“order by”?如果我as在 JPQL 中使用,SQL 不会改变。

Boh*_*ian 5

在 中命名计算order by

select
    o.key,
    count(o.id),
    sum(o.errors)
from MyEntity o
group by o.key
order by sum(o.errors)
Run Code Online (Sandbox Code Playgroud)