Spring Data JPA - Between Query Method Example

In this tutorial, we will learn how to write a Between query method or finder method using Spring Data JPA.

Example: Let's write the Spring Data JPA query method to find or retrieve products based on the price range ( start price and end price).

Maven Dependencies

Create a Spring boot project and add the following maven dependencies to it:



Product Entity

Let's first create a Product entity class and add the following content to it:

package com.springdatajpa.springboot.entity;

import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;

        name = "products",
        schema = "ecommerce",
        uniqueConstraints = {
                        name = "sku_unique",
                        columnNames = "stock_keeping_unit"
public class Product {

            strategy = GenerationType.IDENTITY
    private Long id;

    @Column(name = "stock_keeping_unit", nullable = false)
    private String sku;

    @Column(nullable = false)
    private String name;

    private String description;
    private BigDecimal price;
    private boolean active;
    private String imageUrl;

    private LocalDateTime dateCreated;

    private LocalDateTime lastUpdated;


Let's create ProductRepository which extends JpaRepository and add the following code to it:
import com.springdatajpa.springboot.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import java.math.BigDecimal;
import java.util.List;

public interface ProductRepository extends JpaRepository<Product, Long> {
     * Returns a products whose price between start price and end price
     * @param startPrice
     * @param endPrice
     * @return
    List<Product> findByPriceBetween(BigDecimal startPrice, BigDecimal endPrice);
Spring Data JPA query method to find or retrieve products based on the price range ( start price and end price):
List<Product> findByPriceBetween(BigDecimal startPrice, BigDecimal endPrice);

Configure MySQL and Hibernate Properties

Let's use the MySQL database to store and retrieve the data in this example and we gonna use Hibernate properties to create and drop tables.

Open the application.properties file and add the following configuration to it:


spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto = create-drop

Make sure that you will create ecommerce database before running the Spring boot application. Also, change the MySQL username and password as per your MySQL installation on your machine.

Testing Spring Data JPA - Between Query Method 

Let's write the JUnit test to test Spring Data JPA - Between Query Method:

import com.springdatajpa.springboot.entity.Product;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.math.BigDecimal;

public class QueryMethodsTest {

    private ProductRepository productRepository;

    void findByPriceBetweenMethod(){
        List<Product> products = productRepository.findByPriceBetween(
                new BigDecimal(100), new BigDecimal(300)

        products.forEach((p) ->{

Here is the output of the above JUnit test:

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course