如何通过JPA / Hibernate中的聚合列进行排序?

时间:2011-09-12 08:01:08

标签: java hibernate jpa

执行此JPQL:

select o.key, count(o.id), sum(o.errors) from MyEntity o
group by o.key

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

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

1 个答案:

答案 0 :(得分:4)

order by

中命名计算
select
    o.key,
    count(o.id),
    sum(o.errors)
from MyEntity o
group by o.key
order by sum(o.errors)