Paging and Sorting with Spring Data JPA
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 JPA. Spring 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 Entity. Hero class have two properties, realName and heroName
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)
Having extended from PagingAndSortingRepository we have the following methods (without coding anything)
Paging and Sorting
After having created our repository, which extends PagingAndSortingRepository, we can recover our data as follow
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
In the same way, we can sort the data, as follow
We can combine paging and sorting too
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
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
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
Also we can combine those queries
for example
returns
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