Foundation · Build
Derived Query Methods
Method names like findBySkuIgnoreCase compile into queries at startup; the parser rejects typos and unknown properties before the app ever serves a request.
Primary source: Spring Data JPA: Query Creation from Method Names
Lesson 6 of 17
1d streakThe Architectural Question
Names past three And clauses stop communicating; at that size an explicit @Query documents intent better than grammar gymnastics.
What this guide proves
- Method Naming
- findBy
- Pagination
How the PartTree query parser interprets method names
Spring Data decomposes method names using keywords into JPQL queries automatically. Knowing the grammar prevents subtle querying bugs.
| Method Name | Keyword | Generated SQL Where Clause |
|---|---|---|
| findByStatus(Status s) | Equals | WHERE status = ? |
| findByCustomerEmailIgnoreCase(String email) | IgnoreCase | WHERE UPPER(customer_email) = UPPER(?) |
| findByCreatedAtBetween(Instant s, Instant e) | Between | WHERE created_at BETWEEN ? AND ? |
| findTop10ByOrderByTotalDesc() | Top / OrderBy | ORDER BY total DESC LIMIT 10 |
Break it deliberately: Invalid property name in method signature
org.springframework.data.mapping.PropertyReferenceException: No property 'statuss' found for type 'OrderEntity'! Did you mean 'status'?
Startup validation
Active Recall Checkpoint
When should you transition from a derived query method to an explicit @Query annotation?
Hint: Consider method name length and readability.
Show answer & explanation
When the method name exceeds 3-4 criteria (e.g. findByStatusAndCustomerEmailAndCreatedAtAfterOrderByTotalDesc), or when joins and projections are required. Long derived query names are unreadable and error-prone.
Derived query selection guidelines
Use it when
Use derived queries for simple 1-2 criteria lookups on indexed columns.
Reconsider when
Avoid derived queries with complex outer joins, subqueries, or dynamic predicates; use JPQL or Specifications.
Working Implementation
Code Walkthrough
Write a derived query to find all active products created after a certain date, sorted by price descending.
public interface ProductRepository extends JpaRepository<Product, UUID> {
Optional<Product> findBySkuIgnoreCase(String sku);
boolean existsBySkuIgnoreCase(String sku);
Page<Product> findByActiveTrueAndNameContainingIgnoreCase(
String name,
Pageable pageable
);
}
// The caller controls page number, size, and a safe mapped sort.
var page = PageRequest.of(0, 20, Sort.by("name").ascending());
Page<Product> result = products
.findByActiveTrueAndNameContainingIgnoreCase("keyboard", page);Synthesis & Production Verification
Deliberate Failure Condition
- Break: Type findByFirtName with a typo
- Observe: PropertyReferenceException at bootstrap
- Repair: Spring Data validates every derived query against entity metadata during startup, which is exactly when you want it to fail.
Production Acceptance Criteria
- Write a findBy method with two conditions plus ordering
- Add Pageable and return Slice or Page results
- Explain what startup validation catches before any test runs
Key Takeaway
The PartTree parser translates method naming conventions into verified JPQL queries at startup, providing type-safe simple lookups.
Architectural & Technical Review
Core Interview Questions
Q01How does the Spring Data PartTree parser process method names?Show answer
It decomposes prefixes like findBy with keywords like Between, And, IgnoreCase into parameterized JPQL queries.
Q02How does Spring Data prevent invalid property names in derived query methods?Show answer
It validates every method name against entity properties during ApplicationContext startup, throwing PropertyReferenceException if a field is missing.
Q03When should derived queries be replaced with explicit @Query annotations?Show answer
When method names become excessively long or when custom joins, groupings, or projections are required.
Production Pitfalls
Critical Anti-Patterns
- Pitfall: Writing unindexed multi-property queries — Derived queries across unindexed columns trigger sequential table scans that degrade as table rows grow.
- Pitfall: Ignoring case sensitivity in string lookups — Querying without IgnoreCase can lead to missed matches in case-insensitive search expectations.
Primary Documentation & Specifications
Lesson Primary References
Supplementary Module Resources