Get total count rows for Pageable custom query in Spring repository

ip696 :

I implement pagination like this:

List<Products> products = productRepository.findAllProducts(productsRequest.getInitiatorType(), "ACTIVE",
      new PageRequest(page, 100, Sort.Direction.DESC, "insertDate"));

But how can I get Total size for this query? Only duplicate query like this?

  @Query("SELECT t FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
  List<OpcClProducts> findAllOpcClProducts(String senderId, String status, Pageable pageable);


  @Query("SELECT COUNT(t) FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
  long getTotalCount(String senderId, String status);

And call 2 query: for totalCount and for data?

Noixes :

Try changing your object to

Page<Products> p .. 

p should have the information you are looking for. :) (Posted here because they wanted me to) ^^'

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459506&siteId=1