@Query
@Query is a Spring Data annotation that associates a repository method with an explicitly declared query for retrieving or modifying data.
Spring Start Here, Second Edition
The @Query annotation declares the query expression that Spring Data uses to implement a repository method.
- How it is used
- Defines SQL explicitly for Spring Data repository methods instead of deriving queries from method names.
- How deep it goes
- Shows named SQL parameters and the @Modifying requirement, but does not detail query execution internals.
- Examples
- Demonstrates selecting accounts by name and updating an account amount.
- What you can do after
- Apply @Query in Spring Data repository methods to specify SQL, and pair data-changing queries with @Modifying.
Read how Spring Start Here, Second Edition explains @Query →
Java Persistence with Spring Data and Hibernate epub
The @Query annotation is a Spring Data repository-method annotation that declares a query to execute instead of deriving one from the method name.
- How it is used
- Declares custom repository queries when method-name derivation is insufficient or unsuitable.
- How deep it goes
- Defines parameter binding, native-query portability, sorting, projections, SpEL, and runtime validation, but not query execution internals.
- Examples
- Uses UserRepository counts, filtered users, sorting, projections, relationship-table counts, and MongoDB equality, regular-expression, range, and field-selection queries.
- What you can do after
- Apply it via Spring Data repository interfaces to implement parameterized JPQL, SQL, native SQL, or MongoDB queries.
Read how Java Persistence with Spring Data and Hibernate epub explains @Query →
Spring in Action, Sixth Edition
@Query is a Spring Data annotation that associates a repository method with an explicitly specified database query.
- How it is used
- Defines repository queries directly when derived method names become unwieldy or insufficient.
- How deep it goes
- Explains explicit JPA queries and contrasts JDBC SQL requirements, without detailing query parsing or execution internals.
- Examples
- Uses a repository method that retrieves orders delivered in Seattle.
- What you can do after
- Apply @Query to define repository methods for custom JPA queries or Spring Data JDBC SQL queries.
Also covered in
What to know first
- JPQLJPQL is an object-oriented query language for expressing queries and updates in terms of persistent entity classes and their properties.
Related concepts
- SQLconfigures · SQL is a standardized declarative language for defining, querying, and manipulating data in relational database systems.
- custom query methodconfigures · A custom query method is a repository method whose query is explicitly specified rather than derived entirely from the method name.
- query methodconfigures · A query method is a method that defines, constructs, refines, or evaluates a database query.
Questions about @Query
What is @Query?
@Query is a Spring Data annotation that associates a repository method with an explicitly declared query for retrieving or modifying data.
Which Manning books explain @Query?
Spring Start Here, Second Edition, Java Persistence with Spring Data and Hibernate epub and Spring in Action, Sixth Edition.
What should I know before learning @Query?
Manning books that teach @Query usually introduce JPQL first.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


