I am creating a pageable query over SQL Server using JPA with Spring Data using this repository call:
@Query("select distinct nat from TABE1 nat " +
"left join TABLE2 natr on natr.ID= nat.ID" +
"left join TABLE3 rec on rec.ID= natr.ID_" +
"left join TABLE4 ew on ew.ID= rec.ID_ " +
"where (ew.status = 'VA' or nat.isM = true) " +
"and nat.expDate = :endTime " +
"and nat.dDate = :endTime"
)
Page<Entity1> findAllActiveH(@Param("endTime") LocalDateTime endOfTime,
Pageable pageable);
Next is the generated query with hibernate
select
distinct t.id as id1_47_,
t.creation_date as...
...ALL column
from
TABLE t
left outer join
..... ALL Joint
where
Conditions...
order by 1,
case
when t.creation_date is null then 1
else 0
end,
t.creation_date desc,
case
when lower(t.id) is null then 1
else 0
end,
lower(t.id) asc offset 0 rows fetch next 10 rows only
The generated query append several 'order by' regarding the pageable object but that throw the following exception
ORDER BY items must appear in the select list if SELECT DISTINCT is specified
and I can't remove the distinct to avoid data duplication any idea how to deal with pagination and distinct in spring data / hibernate and SQL Server?
Thanks