liveBook · Manning

@Query

As explained in 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.

@Query declares a custom repository query, so the method name need not follow query-derivation conventions. Spring Data JPA supports JPQL and native SQL; Spring Data JDBC uses SQL; Spring Data MongoDB uses MongoDB query filters. section 4.7 section 15.2.4 section 17.3.4

Common capabilities include:

  • Bind parameters positionally or by name in JPA; JDBC parameters are named and require @Param, while MongoDB uses placeholders such as ?0 and ?1. section 4.7 section 15.2.4 section 17.3.4
  • Use native SQL in JPA with nativeQuery = true, but accept reduced database portability; JDBC custom queries are SQL and therefore vendor-specific. section 4.7 section 15.2.4
  • Add sorting and projections in JPA, including aliases and selected attributes; MongoDB supports field inclusion and exclusion through fields. section 4.7 section 17.3.4
  • Use SpEL's entityName variable in JPA queries to resolve the entity mapped by @Entity. section 4.7

Named parameters in a repository

A Spring Data JPA repository can define `@Query("select u from User u where u.level = :level and u.active = :active")` with `findByLevelAndActive(@Param("level") int level, @Param("active") boolean active)`. The same method shape in Spring Data JDBC uses SQL: `SELECT * FROM USERS WHERE LEVEL = :LEVEL AND ACTIVE = :ACTIVE`.

Custom count for a relationship table

A Spring Data JDBC repository can count links without deriving a property that is absent from the mapped class: `@Query("SELECT COUNT(*) FROM USERS_ADDRESSES WHERE USER_ID = :USER_ID") int countByUserId(@Param("USER_ID") Long userId);`

The flexibility has a validation tradeoff: malformed derived-query names fail while loading the application context, whereas incorrect @Query definitions fail when the method executes. section 4.7

How Java Persistence with Spring Data and Hibernate epub covers it

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 it assumes
Assumes repository interfaces and basic persistence mappings, while teaching custom query syntax, parameter binding, projections, and portability consequences.
What you can do after
Apply it via Spring Data repository interfaces to implement parameterized JPQL, SQL, native SQL, or MongoDB queries.
How it connects
Connects query derivation with JPQL, native SQL, SQL, MongoDB query filters, @Param, Sort, SpEL, projections, and Query by Example.

Also explained in

Compare how 2 books explain @Query →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.