Page.getTotalElements() returns the number of total data in table not the number of result of query

447 views Asked by At

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);
0

There are 0 answers