liveBook · Manning

Derived query method

As explained in Spring Start Here, Second Edition

Query derivation is the technique of generating a database query by interpreting a repository method's name according to framework naming rules.

Custom repository searches often need SQL. A derived query method lets Spring Data infer a query from the method name, avoiding handwritten SQL for simple searches. section 14.3

Listing 14.5 Adding a repository operation to get all the accounts with a specified name
public interface AccountRepository 
  extends CrudRepository<Account, Long> {
 
  List<Account> findAccountsByName(String name);
  
}

See how `findAccountsByName` declares an account-name lookup in the repository interface. section 14.3

Reading the method name

`find` signals a SELECT; `ByName` supplies the condition `WHERE name = ?`. Because the method is in `AccountRepository`, `findByName` can also work. Explicit SQL with `@Query` is presented as the preferred alternative. section 14.3

How Spring Start Here, Second Edition covers it

How deep it goes
Maps `find` and `ByName` to SELECT and WHERE, then contrasts explicit SQL without detailing a broader naming grammar.
Examples
Uses `findAccountsByName` and `findByName` to demonstrate account-name searches.
What you can do after
Apply Spring Data naming rules to declare an account-name lookup without writing its SQL query.
How it connects
Links Spring Data repositories with SQL SELECT, WHERE, and explicit `@Query` declarations.

Also explained in

Compare how 2 books explain derived query method →

Related concepts

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