0

hi Iam using spring boot, JPA

I used Pageable objects to do Pagination

and when I get each Page, I got totalElements by using getTotalElements()

I expected to get the number of searching result, but I got the total number of table's record.

for example, if i have 14 records in my db table and the number of searching result is 6 ,

  • request page =0 size =20

getTotalElements() method returns 6 and getNumberOfElements() method returns 6 so it has no problem in this case

  • request page =1 size =20

    getTotalElements() method returns 14 and getNumberOfElements() method returns 0 so it has problem in this case

  • request page =0 size =4

    getTotalElements() method returns 14 and getNumberOfElements() method returns 4 so it has problem in this case

  • request page =1 size =4

    getTotalElements() method returns 6 and getNumberOfElements() method returns 2 so it has no problem in this case

I don't know why it happens...

data from query is correct but i have no idea why getTotalElements returns the total elements of db records. (and even it's not all the time..)

service layer

 Optional<Page<Club>> clubList = searchRepository.findByConditionsOrderByDate(pointWKT, distance * 1000, state, category, memberNum, regexpTag, pageable);

//and I call getTotalElements() here

repository

 @Query(nativeQuery = true, value = searchDefaultQuery+ WhereOptionQuery +
            "ORDER BY created_date DESC",
            countQuery = "SELECT COUNT(club_id) FROM club")
    Optional<Page<Club>> findByConditionsOrderByDate (@Param("standard_point") String point, @Param("distance")Integer distance,
                                                      @Param("state")String state, @Param("category")String category, @Param("memberNum")Integer memberNum,
                                                      @Param("tags")String tags, Pageable pageable);
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 1
    Your `countQuery` does not have a WHERE clause so it counts every row in the table. In newer Spring Data JPA versions, you [don't need to specify a countQuery for a native query](https://stackoverflow.com/questions/38349930/spring-data-and-native-query-with-pagination). – Chin Huang Jul 21 '23 at 18:44
  • thank you so much. by deleting countQuery, It works well – GyeongEun Kim Jul 22 '23 at 09:24

0 Answers0