hibernate pagination

  1. Overview
    Pagination is often helpful when we have a large dataset and we want to present it to the user in smaller chunks.

Also, we often need to sort that data by some criteria while paging.

In this tutorial, we’ll learn how to easily paginate and sort using Spring Data JPA.

Further reading:
Spring Data JPA @Query
Learn how to use the @Query annotation in Spring Data JPA to define custom queries using JPQL and native SQL.
Read more →
Derived Query Methods in Spring Data JPA Repositories
Explore the query derivation mechanism in Spring Data JPA.
Read more →
2. Initial Setup
First, let’s say we have a Product entity:

@Entity
public class Product {
     
    @Id
    private long id;
    private String name;
    private double price; 
 
    // constructors, getters and setters 
 
}

as our domain class. Each of our Product instances has a unique identifier – id, its name and its price associated with it.

  1. Creating a Repository
    To access our Products, we’ll need a ProductRepository:
public interface ProductRepository extends **PagingAndSortingRepositor**y<Product, Integer> {
 
    List<Product> findAllByPrice(double price, Pageable pageable);
}

By having it extend PagingAndSortingRepository, we get findAll(Pageable pageable) and findAll(Sort sort) methods for paging and sorting.

Or, we could have chosen to extend JpaRepository instead, as it extends PagingAndSortingRepository, too.

Once we extend PagingAndSortingRepository, we can add our own methods that take Pageable and Sort as parameters, as we did here with findAllByPrice.

Let’s take a look at how to paginate our Products using our new method.

  1. Pagination
    Once we have our repository extending from PagingAndSortingRepository, we just need to:

Create or obtain a PageRequest object, which is an implementation of the Pageable interface
Pass the PageRequest object as an argument to the repository method we intend to use
We can create a PageRequest object by passing in the requested page number and the page size.

Here, the page counts starts at zero:

Pageable firstPageWithTwoElements = PageRequest.of(0, 2);

Pageable secondPageWithFiveElements = PageRequest.of(1, 5);
In Spring MVC, we can also choose to obtain the Pageable instance in our controller using Spring Data Web Support.

Once we have our PageRequest object, we can pass it in while invoking our repository’s method:

Page<Product> allProducts = productRepository.findAll(firstPageWithTwoElements);
 
List<Product> allTenDollarProducts = 
  productRepository.findAllByPrice(10, secondPageWithFiveElements);
The findAll(Pageable pageable) method by default returns a Page<T> object.

However, we can choose to return either a Page, a Slice or a List from any of our custom methods returning a paginated data.

A Page instance, in addition to having the list of Products, also knows about the total number of available pages. It triggers an additional count query to achieve it. To avoid such an overhead cost, we can instead return a Slice or a List.

A Slice only knows about whether the next slice is available or not.

  1. Pagination and Sorting
    Similarly, to just have our query results sorted, we can simply pass an instance of Sort to the method:
Page<Product> allProductsSortedByName = productRepository.findAll(Sort.by("name"));

However, what if we want to both sort and page our data?

We can do that by passing the sorting details into our PageRequest object itself:

Pageable sortedByName = 
  PageRequest.of(0, 3, Sort.by("name"));
 
Pageable sortedByPriceDesc = 
  PageRequest.of(0, 3, Sort.by("price").descending());
 
Pageable sortedByPriceDescNameAsc = 
  PageRequest.of(0, 5, Sort.by("price").descending().and(Sort.by("name")));

Based on our sorting requirements, we can specify the sort fields and the sort direction while creating our PageRequest instance.

As usual, we can then pass this Pageable type instance to the repository’s method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值