Spring Data JPA Distinct Query Method Example

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

Spring Data JPA allows developers to define query methods directly in the repository interface. By following a specific naming convention, you can create queries without writing any actual SQL or JPQL code. Among these capabilities is the use of the keyword Distinct to ensure that the result has distinct values.

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 jakarta.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:
package com.springdatajpa.springboot.repository;

import com.springdatajpa.springboot.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {
     * Return the distinct product entry whose name is given as a method parameter
     *  If no product entry is found, this method returns null.
    Product findDistinctByName(String name);
Query method to find or retrieve a unique product by name:
    Product findDistinctByName(String name);

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.hibernate.ddl-auto = create-drop

Make sure that you will create an 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 - Distinct Query Method

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

package com.springdatajpa.springboot.repository;

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;

public class QueryMethodsTest {

    private ProductRepository productRepository;

    void findDistinctByNameMethod(){

        Product product = productRepository.findDistinctByName("product 1");

Here is the output of the above JUnit test:

Note that the Spring Data JPA (uses Hibernate as a JPA provider) generated SQL statements in a console.

Related Spring Data JPA Examples