Abhishek
Abhishek

Reputation: 1267

Getting wrong number of pages in Spring Boot

I am doing paging in spring boot. I am having 32 records on a list. I am trying to get these lists in pages with a page size of 5. When I check for pages from 0 to 3, I am getting correct total elements(5 on each page). But when I check for the last page, it is giving me one less element in the list(1 element on the last page, but it should return 2) and also, the total count of elements is reduced by one.

@Query(value = "SELECT DISTINCT s FROM Students s JOIN FETCH s.Teachers WHERE s.Teachers IN (:Teachers)", countQuery = "SELECT count(DISTINCT s) FROM Students s" +
             " WHERE s.Teachers IN (:Teachers)")
    Page<Students> findByStd(@Param("Teachers") List<Teacher> Teacher, Pageable pageable);

Upvotes: 2

Views: 2239

Answers (1)

Vy Do
Vy Do

Reputation: 52516

SELECT DISTINCT s 
FROM Students s JOIN FETCH s.Teachers 
WHERE s.Teachers IN (:Teachers)
SELECT count(DISTINCT s) 
FROM Students s
WHERE s.Teachers IN (:Teachers)

Let's see

FROM Students s JOIN Teachers

is different with

FROM Students s WHERE s.Teachers IN (:Teachers)

thereforce, pageNumber is wrong.

Upvotes: 1

Related Questions