The @Query annotation lets you attach your own JPQL or native SQL query to a Spring Data JPA repository method. Use it when a derived query name such as findByLastNameAndStatus becomes unreadable or cannot express the query, for example with joins, projections, aggregates or database-specific SQL.
Spring Data validates JPQL queries at startup, binds parameters for you and maps the result to entities, projections or scalar values.
The annotation goes on a method of a repository interface. By default the value is JPQL, which uses entity and field names, not table and column names.
You do not write an implementation. Spring Data creates a proxy that runs the query. If the JPQL is invalid, the application context fails at startup, which is much better than failing on the first request.
You can bind by position or by name. Named parameters are clearer and survive reordering of method arguments.
@Param is required for named parameters unless you compile with the -parameters flag (Spring Boot's Maven and Gradle plugins enable it by default). For LIKE searches, you can use SpEL-style wildcards in the query: where u.name like %:name%. Spring escapes the bound value correctly.
Set nativeQuery = true to run plain SQL against the database. Use this for features JPQL lacks, such as window functions, JSONB operators or vendor hints.
Native queries are not validated at startup and tie you to one database dialect. Sorting by a Sort argument is limited with native queries, and pagination needs a separate countQuery:
@Query is read-only by default. For update and delete statements add @Modifying and run the method inside a transaction.
The return value is the number of affected rows. These statements bypass the persistence context, so entities already loaded may be stale. clearAutomatically = true clears the context after the update to avoid that.
You can return interfaces, records or scalar values instead of whole entities.
Adding a Pageable parameter applies paging, and a Sort parameter appends an order by clause for JPQL. Use the fully qualified record name in the constructor expression.
| Situation | Better choice |
|---|---|
| Simple filters on one or two fields | Derived query method |
| Joins, aggregates, custom projections | @Query with JPQL |
| Vendor-specific SQL | @Query with nativeQuery = true |
| Dynamic, optional filters | Specifications or Querydsl |
| Reused named query on the entity | @NamedQuery |
Do not build queries by concatenating user input. Always use bound parameters, which protect against SQL injection.
JPQL. You must set nativeQuery = true to send raw SQL to the database.
For named parameters you need @Param on each method argument, or you must compile with -parameters. Names in the query and annotation must match exactly.
Modifying queries run directly against the database and skip the persistence context. Use clearAutomatically = true or reload the entity.
Each Spring Data module has its own @Query annotation with a different query language, so import the one from your module. This article covers org.springframework.data.jpa.repository.Query.
@Query gives you full control of the query behind a repository method. Use JPQL for portable, startup-validated queries, native SQL when you need database features, @Modifying for bulk updates and deletes, and always bind parameters instead of concatenating strings.