Since the Version 6.2.4 the sql is wrong if you have follwing
- EntityA
- EntityB
- EntityA has many-to-one relationship to EntityB
- you select distinct EntityA.entityB and sets max results to something e.g. 2
Real Example attached hql:
var list = em.createQuery("select distinct a.entityB from EntityA a ", EntityA.class)
.setMaxResults(2)
.getResultList();
Produced sql was correct in 6.2.1, 6.2.3
select * from (select distinct e2_0.tableBId c0 from TABLE_A e1_0 join TABLE_B e2_0 on e2_0.tableBId=e1_0.tableBId) where rownum<=?
Since 6.2.4 the produces SQL is wrong
select * from (select distinct e2_0.tableBId c0,rownum rn from TABLE_A e1_0 join TABLE_B e2_0 on e2_0.tableBId=e1_0.tableBId) r_0_ where r_0_.rn<=? order by r_0_.rn
this is wrong because if you include rownum as column and distinct over it then all rows will become unique even the real duplicates |