Skip to main content

Ensure Pageable Parameters are Used in Methods Returning Page or Slice

High
correctnessmaintainability

What is it?

This practice concerns ensuring that any methods returning Page or Slice in Spring Data Repository include a Pageable parameter, which enables proper pagination.

Why apply it?

Omitting the Pageable parameter in such methods can lead to runtime exceptions and inefficient handling of data sets, as the repository will not be able to paginate results properly.

How to fix it?

Add a Pageable parameter to method signatures in Spring Data Repositories that return Page or Slice. This ensures that the pagination support is correctly implemented for query results.

Examples

Example 1:

Negative

In this negative example, the method returns a Page but does not include a Pageable parameter, which will cause issues with pagination.

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;

interface ItemRepository extends JpaRepository<Item, Long> {
Page<Item> findItemsByCategory(String category); // Noncompliant
}

Example 2:

Positive

In this positive example, the method correctly includes a Pageable parameter to support pagination of the Page result.

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

interface ItemRepository extends JpaRepository<Item, Long> {
Page<Item> findItemsByCategory(String category, Pageable pageable); // Compliant
}

Negative

This negative example demonstrates a Slice return type without a suitable Pageable parameter, which will throw an error during execution.

import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;

interface ProductRepository extends JpaRepository<Product, Long> {
Slice<Product> findActiveProducts(); // Noncompliant
}

Example 3:

Positive

In this positive example, a method returning a Slice includes the Pageable parameter, ensuring proper handling of slice results.

import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

interface ProductRepository extends JpaRepository<Product, Long> {
Slice<Product> findActiveProducts(Pageable pageable); // Compliant
}