liveBook · Manning

@Query

As explained in Spring in Action, Sixth Edition

@Query is a Spring Data annotation that associates a repository method with an explicitly specified database query.

@Query associates a repository method with an explicitly specified database query. Use it when a method name cannot express the required query clearly or completely. section 3.3.4

Querying Seattle orders

@Query("Order o where o.deliveryCity='Seattle'") List<TacoOrder> readOrdersDeliveredInSeattle(); The method name can be chosen freely because the annotation defines the query. With JPA, @Query can express queries that are difficult or impossible to derive from a method name. section 3.3.4

Choose the query form according to the Spring Data technology:

  • JPA supports explicitly written JPA queries through @Query. section 3.3.4
  • Spring Data JDBC requires @Query for every custom query, and those queries must use SQL rather than JPA query syntax. section 3.3.4

How Spring in Action, Sixth Edition covers it

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 it assumes
Assumes repository methods and basic query syntax while contrasting method-name derivation with explicit queries.
What you can do after
Apply @Query to define repository methods for custom JPA queries or Spring Data JDBC SQL queries.
How it connects
Connects Spring Data repositories with JPA, Spring Data JDBC, derived query methods, and SQL.

Also explained in

Compare how 2 books explain @Query →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.