Skip to content

Spring pagination total count

Spring pagination total count

1. Overview

In this article, we will learn to get the total count of elements through Spring pagination. To learn about Spring JPA, refer to this article.

2. Spring Boot Pagination

When we are populating a large dataset from the database, it is difficult to read all the records on the same page. Using pagination, we can divide the result-set into multiple pages, and that increases the readability of the result sets.

3. Entity class

First, let’s create an entity class that represents the Customer table in the database. We specified the primary key of the entity class by annotating the field id with @Id.

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.Id;

import javax.persistence.Entity;

@Entity
@AllArgsConstructor
@Data
public class Customer {

    @Id
    private String id;
    private String name;

}

Here, we have annotated the Entity class with annotations @AllArgsConstructor and @Data from the Lombok library. The @AllArgsConstructor generates a constructor with all arguments whereas @Data generates the getters and setters. This helps to minimize the boilerplate code.

However, you can also manually create a constructor and getters/setters rather than using the Lombok library.

4. A Repository class with Pagination

Now, let’s create a Repository class for the Spring JPA. The annotation @Repository is a specialization of @Component annotation which is used to show that the implementing class provides the mechanism for storage, retrieval, update, delete and search operation on DB objects.

The CrudRepository interface provides sophisticated CRUD functionality for the entity class that is being managed. The PagingAndSortingRepository extends the CrudRepository and adds additional methods findAll(Pageable pageable) and findAll(Sort sort) methods to ease paginated and sorted access to entities.

The following CustomerRepository extends the PagingAndSortingRepository interface.

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerRepository extends PagingAndSortingRepository<Customer, String> {

    Page<Customer> findAllByName(String name, Pageable pageable);
}

You can also use the JpaRepository interface. The JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository. Because of this inheritance, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository.

@Repository
public interface CustomerRepository extends JpaRepository<Customer, String> {

    List<Customer> findAllByName(String name, Pageable pageable);
}

5. Retrieve total count using the Page object

The Pageable is an abstract interface for pagination information. You can perform pagination by following the steps:

  1. Create a PageRequest object which is an implementation of the Pageable instance.
  2. Pass the PageRequest object as an argument to the repository methods

You can create a PageRequest object using the of method which takes page number and size of the page that needs to be returned as arguments.

PageRequest of(int page, int size)

First, let’s understand the default findAll(Pageable pageable) method provided by the paging interface.

As mentioned earlier, the PagingAndSortingRepository interface provides the method findAll(Pageable pageable) that returns a Page of entities retrieved using the paging condition provided in the Pageable object.

For example, the following code returns the first page by a page size of 20.

Page<Customer> customers = customerRepository.findAll(PageRequest.of(0, 20));

The above findAll function returns the Page of Customer entities by default that contains additional information such as the total number of elements, total number of pages.

In the following code, we invoked the getTotalElements method of the Page to retrieve the total number of elements.

long totalElements = customers.getTotalElements();

6. Conclusion

To sum up, we have discussed getting the total count of elements through Spring pagination along with examples. You can find the code samples of this article in our GitHub repository.

3 thoughts on “Spring pagination total count”

  1. Pingback: Pageable sort spring boot - TedBlob

  2. Pingback: Spring jpa get count - TedBlob

  3. Pingback: JPA count query with WHERE clause - TedBlob

Leave a Reply

Your email address will not be published. Required fields are marked *