liveBook · Manning

@Query

@Query is a Spring Data annotation that associates a repository method with an explicitly declared query for retrieving or modifying data.

How 3 Manning books explain it

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.

Read how Spring in Action, Sixth Edition explains @Query →

Also covered in

What to know first

Related concepts

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.