Using @Named Queries

Great! Now, let’s explore @NamedQuery in Spring Data JPA, which allows defining predefined queries directly in the entity class.


🎯 Learning Objectives

βœ… Learn how to define named queries inside the @Entity class
βœ… Use JPQL to create reusable queries
βœ… Execute named queries in StudentRepository


πŸ”Ή Step 1: Define Named Queries in Student Entity

We define @NamedQuery above the entity class using JPQL (Java Persistence Query Language).

package com.example.model;

import jakarta.persistence.*;

@Entity
@Table(name = "students")
@NamedQueries({
    @NamedQuery(
        name = "Student.findByEmail",
        query = "SELECT s FROM Student s WHERE s.email = :email"
    ),
    @NamedQuery(
        name = "Student.findByLastName",
        query = "SELECT s FROM Student s WHERE s.lastName = :lastName"
    ),
    @NamedQuery(
        name = "Student.updateEmailById",
        query = "UPDATE Student s SET s.email = :email WHERE s.id = :id"
    )
})
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String firstName;
    private String lastName;
    private String email;

    // Constructors, Getters, and Setters
    public Student() {}

    public Student(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    // Getters and Setters omitted for brevity
}

βœ… Why?

  • @NamedQueries groups multiple named queries together.
  • @NamedQuery(name = "...", query = "...") defines a reusable JPQL query.
  • Queries can filter, update, or delete data efficiently.

πŸ”Ή Step 2: Use Named Queries in StudentRepository

Now, we reference named queries in the repository interface.

package com.example.repository;

import com.example.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {

    // Using NamedQuery defined in the Student entity
    @Query(name = "Student.findByEmail")
    Student findByEmail(@Param("email") String email);

    @Query(name = "Student.findByLastName")
    List<Student> findByLastName(@Param("lastName") String lastName);
}

βœ… Why?

  • JPQL query is already defined in the entity, so we only reference it here.
  • @Query(name = "...") fetches the query from @NamedQuery.

πŸ”Ή Step 3: Test in Main.java

package com.example;

import com.example.model.Student;
import com.example.service.StudentService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Bean
    public CommandLineRunner demo(StudentService studentService) {
        return args -> {
            // Save some students
            studentService.saveStudent(new Student("Alice", "Smith", "alice@example.com"));
            studentService.saveStudent(new Student("Bob", "Smith", "bob@example.com"));

            // Find student by email
            Student student = studentService.getStudentByEmail("alice@example.com");
            System.out.println("Found: " + student.getFirstName() + " " + student.getLastName());

            // Find students by last name
            System.out.println("Students with last name 'Smith': " + studentService.getStudentsByLastName("Smith"));
        };
    }
}

βœ… Why?

  • Demonstrates how @NamedQuery is used in a real-world scenario.
  • Fetches a student by email and lists students with the same last name.

πŸ“Œ Key Takeaways

FeatureExplanation
@NamedQueryDefines a reusable query inside an entity class
@NamedQueriesGroups multiple named queries
@Query(name = "...")References the named query in Spring Data JPA
AdvantageBetter organization and query reuse

πŸš€ Next Steps

βœ… Modify named queries to support pagination & sorting
βœ… Implement an update query using @Modifying and @NamedQuery

πŸ‘‰ Would you like to see an example of modifying named queries for updates? 😊