Spring Data JPA - @NamedQuery Example

In this tutorial, we will learn how to create a Named Query using @NamedQuery annotation in Spring Data JPA.

Well, we use @NamedQuery annotation to define Named JPQL query.

Steps to Define Named JPQL Query

If we want to create a Named JPQL query, we must follow these steps:

1. Annotate the entity with the @NamedQuery annotation from JPA:

        name = "Product.findByPrice",
        query = "SELECT p from Product p where p.price = :price"
class Product{ // code goes here}

2. Use @NamedQuery annotation’s name attribute to set the name of the named query:

        name = "Product.findByPrice",
        query = "SELECT p from Product p where p.price = :price"

3. Use @NamedQuery annotation’s query attribute to set the JPQL query the value:

        name = "Product.findByPrice",
        query = "SELECT p from Product p where p.price = :price"

4. Use named query name in a Spring Data JPA Repository:

    // Define Named JPQL query
    Product findByPrice(@Param("price") BigDecimal price);

Let's create a Spring boot project from the scratch and let's understand the usage of @NamedQuery annotation in Spring Data JPA.

1. Creating Spring Boot Project

Spring Boot provides a web tool called https://start.spring.io to bootstrap an application quickly. Just go to https://start.spring.io and generate a new spring boot project.

Use the below details in the Spring boot creation:

Project Name: spring-data-jpa-course

Project Type: Maven

Choose dependencies:  Spring Data JPA, MySQL Driver, Lombok

Package name: net.javaguides.springboot

2. Maven Dependencies

Here is the complete pom.xml for your reference:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>




3. Configure MySQL database

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 a demo database before running the Spring boot application.
Also, change the MySQL username and password as per your MySQL installation on your machine.

4. Create JPA Entity - Product.java

Let's create a entity package inside a base package "net.javaguides.springboot". 

Within the entity package, create a Product class with the following content:
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

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


        name = "Product.findByPrice",
        query = "SELECT p from Product p where p.price = :price"
        name = "products",
        schema = "ecommerce",
        uniqueConstraints = {
                        name = "sku_unique",
                        columnNames = "stock_keeping_unit"
public class Product {

            strategy = GenerationType.SEQUENCE,
            generator = "product_generator"

            name = "product_generator",
            sequenceName = "product_sequence_name",
            allocationSize = 1
    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;

Note that we are using Lombok annotations to reduce the boilerplate code.

5. Create Spring Data JPA Repository

The next thing we’re gonna do is to create a repository to access Product entity data from the database.

The JpaRepository interface defines methods for all the CRUD operations on the entity, and a default implementation of the JpaRepository called SimpleJpaRepository.

Let's create a repository package inside a base package "net.javaguides.springdatarest".

Within the repository package, create a ProductRepository interface with the following content:

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

import java.math.BigDecimal;

public interface ProductRepository extends JpaRepository<Product, Long> 
    // Define Named JPQL query
    Product findByPrice(@Param("price") BigDecimal price);
6. Testing Named JPQL Query

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.math.BigDecimal;
import java.util.List;

public class NamedQueriesTest {

    private ProductRepository productRepository;

    void namedJPQLQuery(){

        Product product = productRepository.findByPrice(new BigDecimal(100));


Related Spring Data JPA Tutorials and Examples

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

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