@Query vs Derived Methods — Choosing the Right Approach
Fast recall
Quick revision
Core answer
Practical judgment on the readability/capability trade-off between derived methods and @Query
Cramming 5-6 conditions into a derived method name instead of a clear @Query, or thinking derived methods can do JOIN/GROUP BY
Give concrete thresholds (3 conditions, ~70 chars) and note aggregates/joins force @Query
Interview-ready guide
Interview answer
findByEmailIgnoreCase is easier to understand as a derived method than as a separate JPQL string. A report with joins, grouped totals, and a DTO projection is clearer as @Query or a custom repository. Derived query keywords support nested properties, sorting, limiting, count, delete, and many predicates, so they are not restricted to three fields. The decision is readability and capability. Native SQL trades portability for database-specific features; JPQL stays in the entity model but still needs tests for generated SQL and performance.
For example, findByStatusOrderByCreatedAtDesc is clear for a simple lookup. A report joining orders and customers with projections and grouped totals is clearer as @Query or a custom repository implementation.
@Query vs Derived Methods — Choosing the Right Approach
Direct Answer
Use a derived method when its supported keywords express the query clearly. Use @Query when the name becomes hard to read or when explicit JPQL/native SQL is needed for projections, complex joins, aggregation, subqueries, or provider/database features. There is no meaningful character-count cutoff, and derived methods can traverse relationships and express more than only one to three fields.
Common Mistakes & Pitfalls
Cramming 5-6 conditions into a derived method name instead of a clear @Query, or thinking derived methods can do JOIN/GROUP BY
Check your understanding
Answer these without looking back at the article.
- How do you return a custom DTO instead of an entity from a @Query?
- Can derived query methods express JOIN queries?
- What are Spring Data JPA projections?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.