In Spring Data JPA, @Query lets you declare the JPQL or native SQL statement that a repository method runs, overriding the query Spring would otherwise derive from the method name. It is the standard way to express joins, aggregates, filtered searches and bulk updates while keeping the query next to the method that uses it.
This article focuses on how the annotation resolves its query, how parameters and SpEL expressions work, and how it relates to named queries, so you can choose the right option for each repository method.
@QueryWhen the application context starts, Spring Data JPA looks up a query for every method on your repository interface. It uses this order by default (the lookup strategy CREATE_IF_NOT_FOUND):
@Query.EntityName.methodName, for example Employee.findByLastName.So @Query always wins over a method name. If the JPQL is invalid, for instance because a field was renamed, startup fails with an error that points at the method. That makes refactoring safer than with plain string queries built at runtime.
JPQL refers to entity classes and attributes, not table and column names.
The first method returns entities. The second returns raw rows; for anything beyond a quick check, return a record or interface projection instead of Object[], as shown below.
There are three ways to pass values into a query.
| Style | Syntax | Notes |
|---|---|---|
| Positional | ?1, ?2 | Order matters; breaks if you reorder arguments |
| Named | :name with @Param("name") | Preferred; reads clearly |
| SpEL | :#{#user.email} | Reads a property of an argument object |
SpEL lets one object carry several filter values. The like %:prefix form adds the wildcard on the Spring side, still as a bound parameter. Never build the query text by concatenating user input; always pass it as a parameter.
Loading whole entities when you only need two columns wastes memory and can trigger lazy-loading. A record or interface projection solves that.
For interface projections the aliases must match the getter names. For the constructor expression, use the fully qualified class name and matching argument types.
Set nativeQuery = true for SQL that JPQL cannot express:
Native queries are not validated at startup and tie you to one database. If the same query is reused from several places, a named query declared once on the entity is an alternative:
Spring Data picks it up automatically for a repository method named findActiveByDepartment. In practice most teams keep queries on the repository with @Query, because the query and its signature stay in one file.
@QueryAdd a Pageable or Sort parameter and Spring Data applies it to your query. For paged queries with joins or group by, provide an explicit countQuery:
Without the countQuery, Spring derives one from your statement, which works for simple queries but can be wrong or slow for complex ones.
@Query required on every repository method?No. Simple lookups such as findByLastName are derived from the method name. Add @Query when the derived name gets long or the logic cannot be named.
@Query?Use JPQL by default; it is portable and validated at startup. Use native SQL only for features your database offers that JPQL lacks.
@Query and @NamedQuery?@NamedQuery is declared on the entity and referenced by name, while @Query sits on the repository method. Both are checked at startup; @Query takes precedence when both exist.
@Query?Yes, add @Modifying and run the method inside a transaction. The method can return int with the number of affected rows.
@Query takes priority over derived and named queries and lets you write JPQL or native SQL for a repository method. Bind values with named parameters or SpEL, return projections to avoid loading full entities, add a countQuery for complex paged queries, and keep derived methods for simple lookups.