Derived Query Methods in Spring Data JPA — Name-Based Query Generation
Fast recall
Quick revision
Core answer
findByLastName(String name) produces WHERE last_name = ? — no SQL, no @Query. Spring parses the name at startup into a query, so a typo or unknown field fails the application boot (fast feedback), not at runtime.Whether you understand name-based query generation and that names are validated at startup against entity fields
Writing @Query JPQL for simple conditions derived methods already handle, or omitting @Transactional on deleteBy
Mention startup-time parsing/validation and the keyword grammar (And, Or, GreaterThan, Between, OrderBy, Top)
Interview-ready guide
Interview answer
Derived query methods are a Spring Data JPA feature where I just write a method signature in my repository interface and Spring generates the query by reading the method name.
For example, if I write findByLastName with a String parameter, Spring builds a select with a where clause on the last name field. I don't write any SQL or annotations. This works because Spring parses the method name at application startup.
It breaks the name into parts — findBy means it's a select, LastName maps to the last name field on my entity, and the parameter becomes a where condition. I can combine conditions with And or Or, like findByEmailAndStatus. There are operator keywords too, like findByAgeGreaterThan, and result shapers like OrderBy, Top, and IgnoreCase.
The mistake Learners make is writing a custom Query annotation for simple lookups that a derived method handles perfectly. Another gotcha is delete methods: if I write deleteByStatus I have to add Transactional, or it throws at runtime.
Something interviewers like to hear is that if my method name doesn't follow the grammar or references a field that doesn't exist, the application fails at startup with a clear error, which is much better than discovering it in production. So derived methods keep repositories clean and readable, and when the names get long with many conditions, that's the signal to switch to a Query annotation with JPQL.
Derived Query Methods in Spring Data JPA — Name-Based Query Generation
Direct Answer
Derived query methods let Spring Data JPA generate the query from the method name. Declaring findByLastName(String name) produces WHERE last_name = ? — no SQL, no @Query. Spring parses the name at startup into a query, so a typo or unknown field fails the application boot (fast feedback), not at runtime.
Code Example
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastName(String lastName); // WHERE last_name = ?
Optional<User> findByEmailAndStatus(String email, String status); // AND
List<User> findByAgeGreaterThanOrderByCreatedAtDesc(int age); // > ? ORDER BY ... DESC
List<User> findByCreatedAtBetween(LocalDateTime start, LocalDateTime end); // BETWEEN
long countByDepartment(String department); // COUNT(*)
boolean existsByEmail(String email); // EXISTS
@Transactional
void deleteByStatus(String status); // DELETE — needs a transaction
}
Common Mistakes & Pitfalls
- Field-name typos —
findByNameLastNameinstead offindByLastName; the property must match the entity field, elsePropertyReferenceExceptionat startup. deleteBywithout@Transactional— throwsTransactionRequiredExceptionat runtime; writes need a transaction.- Overusing derived methods —
findByFirstNameAndLastNameAndAgeAndDepartmentAndStatusis unreadable; switch to@Querypast ~3 conditions. - Reaching for
@Querytoo early — for simplefindByEmail, a derived method is cleaner.
Check your understanding
Answer these without looking back at the article.
- What keywords can you use in derived query method names?
- When should you use @Query instead of a derived query method?
- How does Spring Data parse and validate derived query method names?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.