In Spring Data JPA, the @Query annotation is used to define custom queries directly on repository methods. This allows you to write your own SQL or JPQL (Java Persistence Query Language) queries for more complex data access patterns, beyond what is automatically generated by Spring Data.
Here's an overview of how to use the @Query annotation, along with examples:
Syntax and Usage
-
Basic Queries: You can define a custom JPQL or SQL query using the @Query annotation.
-
Named Parameters: Utilize named parameters (:parameterName) for better readability.
-
Native Queries: You can also run native SQL queries by setting the nativeQuery attribute to true.
Example
Let's say you have an Employee entity class:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
private double salary;
// Getters and setters
}
You can create a repository interface for the Employee entity:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
// Example of a JPQL query
@Query("SELECT e FROM Employee e WHERE e.department = :department")
List<Employee> findByDepartment(@Param("department") String department);
// Example of a native SQL query
@Query(value = "SELECT * FROM employee WHERE salary > :minSalary", nativeQuery = true)
List<Employee> findEmployeesWithSalaryGreaterThan(@Param("minSalary") double minSalary);
// Example of a custom JPQL update query
@Modifying
@Query("UPDATE Employee e SET e.salary = :salary WHERE e.id = :id")
int updateEmployeeSalary(@Param("id") Long id, @Param("salary") double salary);
}
Explanation of the Examples
-
JPQL Query:
- The first method
findByDepartment uses JPQL to fetch employees based on their department.
- The parameter
:department is replaced with the actual method argument.
-
Native SQL Query:
- The second method
findEmployeesWithSalaryGreaterThan uses a native SQL query to find employees with a salary greater than a specified minimum.
- The
nativeQuery = true attribute indicates that the query is based on native SQL.
-
Update Query:
- The third method
updateEmployeeSalary demonstrates how to perform an update query.
- Note the use of the
@Modifying annotation, which is required for any operation that modifies the database (like insert, update, or delete).
Important Points
- When using
@Query, ensure that the query is properly formed according to JPQL or SQL syntax.
- For custom update/delete queries, always include the
@Modifying annotation.
- Use the
@Param annotation to bind method parameters to query parameters for better clarity.
Conclusion
The @Query annotation in Spring Data JPA is a powerful feature that enables you to define custom queries efficiently. By using it alongside Spring's repository abstraction, you can interact with your database in a clean and organized manner while still leveraging the advantages of ORM (Object-Relational Mapping).