Derived Query Methods - Queries From Method Names
Derived Query Methods - Queries From Method Names
Spring Data JPA can parse the name of a repository method and generate the SQL query for you automatically. Instead of writing @Query annotations or JPQL strings, you simply name the method following a convention and Spring does the rest. This feature is called derived query methods.
How Method Name Parsing Works
The method name is split into a parts-based structure. The first part is the keyword that defines the operation. Then come the property names from your entity, connected by operators like And and Or:
Method name: findByTitleContainingAndAuthor
| | |
| | +-- property: author
| +-- property: title, operator: containing (LIKE %..%)
+-- keyword: findBy = SELECT ... WHERE
Spring parses this into a JPQL query: SELECT b FROM Book b WHERE b.title LIKE :title AND b.author = :author. You never write that query yourself.
Common Keywords
There are dozens of supported keywords. Here are the most frequently used ones in practice:
findByTitle(String title) -- WHERE title = ?
findByTitleContaining(String partial) -- WHERE title LIKE %?%
findByTitleStartingWith(String prefix) -- WHERE title LIKE ?%
findByPriceLessThan(double price) -- WHERE price < ?
findByPriceBetween(double min, double max) -- WHERE price BETWEEN ? AND ?
findByAuthorIsNull() -- WHERE author IS NULL
findByOrderByTitleDesc() -- ORDER BY title DESC
findTop3ByOrderByPriceDesc() -- LIMIT 3 (top 3)
existsByIsbn(String isbn) -- returns boolean
countByCategory(String category) -- returns long
deleteByStatus(OrderStatus status) -- DELETE ... WHERE status = ?
Defining Derived Queries in the Repository
Just declare the methods in your repository interface. Spring generates the implementation at startup:
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByAuthor(String author);
List<Book> findByTitleContaining(String keyword);
List<Book> findByPriceBetween(double min, double max);
Page<Book> findByCategory(String category, Pageable pageable);
boolean existsByIsbn(String isbn);
}
Notice that the return type can be a single entity, a List, a Page, a boolean, or a long. Spring handles the type mapping. If you pass a Pageable as the last parameter, the result is automatically paginated.
Real-World Scenario
A library management system needs to search books by title, filter by author, find books within a price range, and check if an ISBN already exists. Instead of writing four separate JPQL queries, you add four method signatures to the repository interface. The entire search layer is built without writing a single line of SQL or JPQL.
Key Points
- Spring Data parses the method name to generate the SQL query automatically.
- Keywords like
Containing,LessThan,Between, andIsNullmap to SQL operators. - You can combine keywords with
And/Orfor multi-conditional queries. - Return types can be
List,Page, single entity,boolean, orlong. - Complex queries that cannot be expressed in method names use
@Queryinstead.