Limiting Query Results with JPA and Spring Data JPA

1. Introduction

In this tutorial, we’re going to learn about limiting query results with JPA and Spring Data JPA.

First, we’ll take a look at the table we want to query as well as the SQL query we want to reproduce.

Then we’ll dive right into how to achieve that with JPA and Spring Data JPA.

Let’s get started!

2. The Test Data

Below we have the table that we’ll be querying throughout this article.

The question we want to answer is, “What is the first occupied seat and who is occupying it?”.

First NameLast NameSeat Number
JillSmith50
EveJackson94
FredBloggs22
RickiBobbie36
SiyaKolisi85

3. The SQL

With SQL we might write a query that looks something like this:

SELECT firstName, lastName, seatNumber FROM passengers ORDER BY seatNumber LIMIT 1;

4. JPA Setup

With JPA, we need an Entity first, to map our table:

@Entity
class Passenger {

    @Id
    @GeneratedValue
    @Column(nullable = false)
    private Long id;

    @Basic(optional = false)
    @Column(nullable = false)
    private String fistName;

    @Basic(optional = false)
    @Column(nullable = false)
    private String lastName;

    @Basic(optional = false)
    @Column(nullable = false)
    private int seatNumber;

    // constructor, getters etc.
}

Next we need a method which encapsulates our query code, implemented here as PassengerRepositoryImpl#findOrderedBySeatNumberLimitedTo(int limit):

@Repository
class PassengerRepositoryImpl {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<Passenger> findOrderedBySeatNumberLimitedTo(int limit) {
        return entityManager.createQuery("SELECT p FROM Passenger p ORDER BY p.seatNumber",
          Passenger.class).setMaxResults(limit).getResultList();
    }
}

In our repository method, we use the EntityManager to create a Query on which we call the setMaxResults() method.

This call to Query#setMaxResults will eventually result in the limit statement appended to the generated SQL:

select
  passenger0_.id as id1_15_,
  passenger0_.fist_name as fist_nam2_15_,
  passenger0_.last_name as last_nam3_15_,
  passenger0_.seat_number as seat_num4_15_
from passenger passenger0_ order by passenger0_.seat_number limit ?

5. With Spring Data JPA

We can also generate our SQL using Spring Data JPA.

5.1. first or top

One way we could approach this is by using method name derivation with the keywords first or top.

We can, optionally, specify a number as the maximum result size that will be returned. If we omit it, Spring Data JPA assumes a result size of 1.

Remembering that we want to know what is the first occupied seat and who is occupying it, we can get it omitting the number in these two ways:

Passenger findFirstByOrderBySeatNumberAsc();
Passenger findTopByOrderBySeatNumberAsc();

If we limit to one instance result, as above, then we can also wrap the result using Optional:

Optional<Passenger> findFirstByOrderBySeatNumberAsc();
Optional<Passenger> findTopByOrderBySeatNumberAsc();

5.2. Pageable

Alternatively, we can use a Pageable object:

Page<Passenger> page = repository.findAll(
  PageRequest.of(0, 1, Sort.by(Sort.Direction.ASC, "seatNumber")));

If we take a look at the default implementation of JpaRepository, the SimpleJpaRepository, we can see that it also calls Query#setMaxResults:

protected <S extends T > Page < S > readPage(TypedQuery < S > query, 
  Class < S > domainClass, Pageable pageable,
  @Nullable Specification < S > spec) {
    if (pageable.isPaged()) {
        query.setFirstResult((int) pageable.getOffset());
        query.setMaxResults(pageable.getPageSize());
    }

    return PageableExecutionUtils.getPage(query.getResultList(), pageable, () -> {
        return executeCountQuery(this.getCountQuery(spec, domainClass));
    });
}

5.3. Comparison

Both of these alternatives will produce the SQL that we are after:

select
  passenger0_.id as id1_15_,
  passenger0_.fist_name as fist_nam2_15_,
  passenger0_.last_name as last_nam3_15_,
  passenger0_.seat_number as seat_num4_15_ 
from passenger passenger0_ order by passenger0_.seat_number asc limit ?

With first and top favoring convention and Pageable favoring configuration.

6. Conclusion

Limiting query results in JPA is slightly different to SQL – we don’t include the limit keyword directly into our JPQL.

Instead, we just make a single method call to Query#maxResults or include the keyword first or top in our Spring Data JPA method name.

As always, you can find the code over on GitHub.

Related posts:

Understanding Memory Leaks in Java
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Hướng dẫn Java Design Pattern – Bridge
Configure a RestTemplate with RestTemplateBuilder
Java Program to Implement Naor-Reingold Pseudo Random Function
A Guide to JPA with Spring
Simplify the DAO with Spring and Java Generics
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Check Multiplicability of Two Matrices
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Hướng dẫn Java Design Pattern – Transfer Object
Java Program to Implement Min Heap
Quick Guide on Loading Initial Data with Spring Boot
Spring Boot - Tomcat Port Number
Spring Security 5 – OAuth2 Login
Java – Reader to Byte Array
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Transactions with Spring and JPA
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Collect a Java Stream to an Immutable Collection
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Implement JobStateReasons API
Show Hibernate/JPA SQL Statements from Spring Boot
Control Structures in Java
Java Program to Perform the Shaker Sort
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement Ternary Heap
Java Program to Implement Strassen Algorithm
Query Entities by Dates and Times with Spring Data JPA
Getting the Size of an Iterable in Java
Using Optional with Jackson
Returning Image/Media Data with Spring MVC