ip696
ip696

Reputation: 7084

Get total count rows for Pageable custom query in Spring repository

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?

Upvotes: 15

Views: 20893

Answers (1)

Noixes
Noixes

Reputation: 1178

Try changing your object to

Page<Products> p .. 

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

Upvotes: 22

Related Questions