@Query declares the SQL that Spring Data runs for a repository method. It avoids relying on method-name rules to derive a query, so the method can be named for its purpose instead. section 14.3
Listing 14.6 Using the @Query annotation to specify the SQL query for an operation
public interface AccountRepository
extends CrudRepository<Account, Long> {
@Query("SELECT * FROM account WHERE name = :name") #A
List<Account> findAccountsByName(String name);
}This repository method selects accounts by name; the :name parameter must immediately follow the colon, with no intervening space. section 14.3
For queries that change data with UPDATE, INSERT, or DELETE, pair @Query with @Modifying. section 14.3
Updating an account
A repository method can use @Query("UPDATE account SET amount = :amount WHERE id = :id") to change an account’s amount; annotate that method with @Modifying. section 14.3


