Paging and Sorting with Spring Data JPA

person-pointing-numeric-print-1342460.jpg

BY: Omar Mendoza

RightSource Developer

Motivation

When we develop applications many times we have to execute searches/queries to obtain specific data from our database(s). In our database(s), maybe, we have a lot of information we want to show or share with one or more of our applications or sometimes with external consumers through APIs. So, an important consideration to do that is how we can show/share those data efficiently. A way to improve how we share data is paging those data, in this way the applications (web app, mobile app, etc.) can consume those data in small parts and not all the information is loaded at the same time.

Spring Data and Spring Data JPA

In this post, I'll show how can we make paging and sorting data with one of the hottest technologies today Spring Data JPASpring Data JPA is one of the Spring Data data access layer projects which make easy create JPA based repositories.

The purpose of Spring Data according to his page is:

"provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.
It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies."

Spring Data JPA Features

  • Sophisticated support to build repositories based on Spring and JPA

  • Support for Querydsl predicates and thus type-safe JPA queries

  • Transparent auditing of domain class

  • Pagination support, dynamic query execution, ability to integrate custom data access code

  • Validation of @Query annotated queries at bootstrap time

  • Support for XML based entity mapping

  • JavaConfig based repository configuration by introducing @EnableJpaRepositories.

Setup

First, we need to create our domain model for our data. It consists of a Hero EntityHero class have two properties, realName and heroName

Captura de Pantalla 2019-12-06 a la(s) 11.15.28.png

Spring Data Repository

To be able to show/share our data, we need to retrieve the data first, so, we need to create a simple interface who extends PagingAndSortingRepository (HeroRepository)

Captura de Pantalla 2019-12-06 a la(s) 11.16.44.png

Having extended from PagingAndSortingRepository we have the following methods (without coding anything)

Captura de Pantalla 2019-12-06 a la(s) 11.17.01.png

Paging and Sorting

After having created our repository, which extends PagingAndSortingRepository, we can recover our data as follow

Captura de Pantalla 2019-12-06 a la(s) 11.19.16.png

First argument of PageRequest is the page number and the second is the size of the set. So, how we can see heroRepository.findAll(pageable); returns a Page<Hero> instance. Page<Hero> instance contains among other information the list of heroes and the total of available pages

Captura de Pantalla 2019-12-06 a la(s) 10.42.54.png

In the same way, we can sort the data, as follow

Captura de Pantalla 2019-12-06 a la(s) 11.19.56.png

We can combine paging and sorting too

Captura de Pantalla 2019-12-06 a la(s) 11.20.06.png

Custom Paging and Sorting

Spring Data JPA provides us a set of methods for basics searches operations but sometimes it is not enough. So, we can create some extra methods to recover our necessaries data. To do this we can create methods into our repository interface. For example, if we want the list of all the heroes with the same real or hero name, our methods look like

Captura de Pantalla 2019-12-06 a la(s) 11.20.17.png

Doing that Spring Data JPA knows what we want. We want all the heroes with a specific real-name or hero-name. So, the name of the method defines our query

Captura de Pantalla 2019-12-06 a la(s) 11.20.30.png

Comparators

Spring Data JPA let us specify many types of queries, using one or more of the next comparators before the attribute name

  • Containing

  • Like

  • IgnoreCase

Captura de Pantalla 2019-12-06 a la(s) 11.22.13.png

Also we can combine those queries

Captura de Pantalla 2019-12-06 a la(s) 11.22.22.png

for example

Captura de Pantalla 2019-12-06 a la(s) 11.22.30.png

returns

Captura de Pantalla 2019-12-06 a la(s) 11.22.39.png

Conclusion

So, with Spring Data Repositories we have a set of methods to retrieve our data from the database. This set of methods is more than enough for most of our purposes.

Spring Data JPA lets us write less code in order to make our JPA queries. The use of comparators is a clear example of how easy is to implement queries to retrieve our data.

The use of comparators is nicer if your query is not too much complicated, but if you need more control or more specific queries or your query is complicated, you can use @Query annotation to specify custom queries.

Further information

https://spring.io/projects/spring-data-jpa

https://docs.spring.io/spring-data/data-commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html

Omar Mendoza