@Query lets you write the query a Spring Data JPA repository method runs, instead of deriving it from the method name. You put JPQL (or native SQL with nativeQuery = true) on the method, bind parameters by name or position, and Spring Data generates the implementation. Use it when a derived query like findByPriceGreaterThan would be too long, too limited, or unreadable.
It also supports modifying statements with @Modifying, projections, and pagination.
Spring Data builds queries from method names such as findByLastNameAndActiveTrue. That works well for simple filters. Reach for @Query when you need:
GROUP BY that method names cannot express.findActiveCustomersWithRecentOrders.JPQL queries entities and their fields, not tables and columns. Prefer named parameters with @Param because they stay correct when you reorder arguments.
Positional parameters (?1, ?2) also work, but named ones are safer. Compiling with the -parameters flag, which Spring Boot's Maven and Gradle plugins enable by default, lets Spring bind names without @Param.
Parameter binding is what protects you from SQL injection. Never build the query by concatenating user input into the string.
Set nativeQuery = true to run plain SQL against the database. Column names then refer to real table columns.
Native queries are tied to one database dialect and bypass some JPA features. Sorting with a Pageable is limited for them, and a paginated native query needs an explicit countQuery:
@Query also runs update and delete statements. Add @Modifying and make the call transactional.
The return value is the number of rows changed. Bulk statements bypass the persistence context, so entities already loaded can be stale. clearAutomatically = true clears the context after the statement, which avoids that surprise.
Fetching only the columns you need is faster than loading whole entities. With JPQL you can build a record directly using a constructor expression.
Spring Data also supports interface-based projections and, for derived queries and @Query, class-based DTO projections, so you can often return a record without the new expression. Use the fully qualified class name in the constructor expression, and check your Spring Data version for the shorter forms.
Add a Pageable or Sort parameter and Spring Data applies paging to a JPQL query.
In JPQL you can reference the entity name with SpEL, which is useful in generic base repositories:
JPQL in @Query is parsed when the application starts, so a typo such as a wrong field name fails fast with a clear error. Native queries are checked only when they run. Common mistakes:
| Mistake | Fix |
|---|---|
| Using table names in JPQL | Use entity names and field names |
Forgetting @Modifying on update or delete | Add @Modifying and @Transactional |
| String concatenation of input | Use bound parameters |
| Paginating a native query without a count query | Provide countQuery |
| Returning stale entities after a bulk update | Use clearAutomatically = true |
Yes. Add a Pageable parameter and supply a countQuery. Sorting through Pageable is limited for native queries, so write the order by clause yourself when needed.
Not when the code is compiled with the -parameters option, which Spring Boot build plugins turn on. Keeping @Param is harmless and makes the binding explicit.
@NamedQuery declares the query on the entity class or in XML, while @Query keeps it next to the repository method. Spring Data supports both, and @Query is usually easier to maintain.
Yes, when you use bound parameters. It becomes unsafe only if you concatenate user input into the query string.
@Query gives you control over the exact JPQL or SQL a Spring Data JPA repository executes. Use named parameters, keep JPQL where possible for portability, add @Modifying with @Transactional for bulk writes, supply a countQuery for native pagination, and project into records when you do not need full entities.