The @Query annotation in Spring Data JPA lets you put your own JPQL or native SQL directly on a repository method. You use it when a derived query such as findByCategoryAndPriceLessThan becomes unreadable, or when you need joins, aggregates, projections or database-specific SQL that method names cannot express.
The annotation lives in org.springframework.data.jpa.repository.Query. Spring validates JPQL queries at startup, so many typos fail fast instead of at runtime.
Spring Data builds queries from method names by default. @Query overrides that: the method name no longer matters, and the string you supply is executed instead.
| Approach | Strength | Limitation |
|---|---|---|
| Derived method name | No code to write | Long names, limited expressiveness |
@Query JPQL | Portable, validated at startup | Limited to what JPQL supports |
@Query native SQL | Full database features | Tied to one database, not validated until run |
| Specifications or Querydsl | Dynamic conditions | More setup |
JPQL works on entities and their fields, not tables and columns. Prefer named parameters with @Param:
Named parameters are clearer than positional ?1 markers and survive reordering. If you compile with -parameters (the default with Spring Boot's Maven and Gradle plugins), @Param can be omitted, but keeping it is harmless.
User input is always bound as a parameter, never concatenated into the string, so these queries are safe from SQL injection.
Set nativeQuery = true to send the SQL straight to the database:
Native queries bypass JPQL validation and are not portable across databases. Sorting through a Sort parameter is not supported for native queries in the same way as for JPQL. Pagination needs a separate countQuery:
For JPQL, pass Pageable or Sort as the last parameter and Spring appends the paging and ordering clauses:
Return only the columns you need with an interface projection or a record:
The constructor expression needs the fully qualified class name. For a looser setup, return an interface with getName() and getPrice() and alias the columns (select p.name as name, p.price as price ...).
Aggregates work the same way:
@Query also supports update and delete statements when combined with @Modifying and a transaction:
Bulk statements bypass the persistence context, so already loaded entities become stale. clearAutomatically = true clears the context after execution. The return value is the number of affected rows.
Spring Data supports SpEL in queries, for example :#{#entityName} to reference the entity name in generic repositories, and :#{#user.id} to read a property from a parameter object.
Best practices:
@Query when the name would exceed readable length.countQuery for paged joins and native queries.Specification or Querydsl rather than many @Query variants.JPQL targets entities and is database independent. Native queries are plain SQL sent to the database and can use vendor features, but they are not portable.
Only if the parameter names are not available at runtime. With the -parameters compiler flag, which Spring Boot build plugins enable, names are detected automatically. Using @Param is still a clear, safe habit.
Bulk updates go straight to the database and skip the persistence context. Use clearAutomatically = true or reload the entity.
Yes. Use a constructor expression in JPQL, an interface projection with aliases, or a record in recent Spring Data versions.
@Query lets you write JPQL or native SQL on repository methods when derived names are not enough. Use named parameters, projections for narrow results, countQuery for pagination, and @Modifying with a transaction for bulk changes.