I am new to Spring Data JPA and have the something below
Page<Object> page = tableARepository.findAll(
(root, criteriaQuery, criteriaBuilder) -> criteriaQuery
.where(sortConfig.getPredicateList(root, criteriaQuery, criteriaBuilder))
.groupBy(root.get("colB"))
.getRestriction(),
sortConfig.getPageable()
);
Everything is correct except for page.getTotalElements()
which return the wrong count and I also found out that it is returning the count without groupBy
.
I need page.getTotalElements()
in order to show the total rows for front end and currently the workaround was I re-query it and get the size of the result which is not very pretty thing to do (the website still in development stage).
I also did print out the hibernate sql query and the select count query did include groupBy
but I am not sure what caused it to return the wrong value.
Assume, tableA data as below:
ColA | ColB | ...
1 A
2 B
3 B
4 C
5 D
And after groupBy
I should have result and count below
ColA | ColB | ...
1 A
2 B
4 C
5 D
totalCount: 4
But somehow I got 5, anyone can help?
NOTE: SortConfig is developed by my colleague to use it for pagination purpose.
EDIT:
select
count(tblA_.tableA_ID) as col_0_0_
from
tableA tblA_
where
tblA_.tableAColC like ?
group by
tblA_.tableAColB