liveBook · Manning

Derived query method

As explained in Spring in Action, Sixth Edition

A derived query method is a repository method whose query behavior is inferred by parsing its name and matching name components to entity properties and query operators.

A derived query method is a repository method whose query is inferred from its name. Spring Data parses the method name, matches components to persisted entity properties, and generates the repository implementation. section 3.3.4

Build a method name from these parts:

  • Start with a verb such as find, read, get, or count. section 3.3.4
  • Add an optional subject, then By, followed by predicates that name entity properties and operators such as And and Between. section 3.3.4
  • For example, findByDeliveryZip matches deliveryZip, while readOrdersByDeliveryZipAndPlacedAtBetween combines a ZIP code with a date range. section 3.3.4
List<TacoOrder> findByDeliveryZip(String deliveryZip);

Use this compact repository declaration to see the simplest derived query. section 3.3.4

List<TacoOrder> readOrdersByDeliveryZipAndPlacedAtBetween(
      String deliveryZip, Date startDate, Date endDate);

Compare the basic method with a derived query combining delivery location and placement dates. section 3.3.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains name parsing, method structure, fetch verbs, count queries, properties, and compound predicates.
Examples
Uses delivery ZIP, delivery dates, delivery names, delivery cities, case-insensitive matching, ordering, and a Seattle query.
What it assumes
Assumes a parameterized CrudRepository and a persisted entity whose properties match method-name components.
What you can do after
Build repository methods that derive queries from entity-property names, predicates, and supported query verbs.
How it connects
Connects repositories, CRUD operations, entity properties, query operators, and domain-specific languages.

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.