Hi,
You can find the unit test here GitHub - smalbequi/hibernate-test-case-bug-orderby: Templates and examples to report issues to Hibernate
With Hibernate 6 and org.hibernate.dialect.H2Dialect dialect, the following query generates a wrong order
by:
select
c.id as clientId,
c.name as clientName,
t.code as typeCode,
g.id as generationId,
sum(e.balance)
from Card e
inner join e.generation g
inner join g.type t
inner join t.client c
group by clientId, typeCode, generationId
order by clientName, typeCode, generationId
Generated SQL visible through log:
select
t1_0.client_id c0,
c2_0.name c1,
g1_0.type_code c2,
c1_0.generation_id c3,
sum(c1_0.balance) c4
from
Card c1_0
join
Generation g1_0
on g1_0.id=c1_0.generation_id
join
CardType t1_0
on t1_0.code=g1_0.type_code
join
Client c2_0
on c2_0.id=t1_0.client_id
group by
c0,
c2,
c3
order by
2,
3,
4
With Hibernate 6 and org.hibernate.dialect.PostgresPlusDialect dialect, the query generates a wrong order by and a wrong group by:
select
t1_0.client_id,
c2_0.name,
g1_0.type_code,
c1_0.generation_id,
sum(c1_0.balance)
from
Card c1_0
join
Generation g1_0
on g1_0.id=c1_0.generation_id
join
CardType t1_0
on t1_0.code=g1_0.type_code
join
Client c2_0
on c2_0.id=t1_0.client_id
group by
1,
3,
4
order by
2,
3,
4