Spring data, find by property of a nested object
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Data JPA can derive repository queries from nested entity properties, which is often the cleanest solution for simple lookups. Instead of writing JPQL immediately, you can describe the property path in the repository method name. This works well when the path is short and the query intent stays obvious.
Derived Query Names Follow Entity Properties
The method name must refer to Java entity property names, not database column names. For example, if an order has a customer field and Customer has an email field, the repository can query that nested path directly.
The repository method can then be written like this:
Spring interprets CustomerEmail as the path customer.email.
Use Underscores When the Path Needs Clarification
Spring can often parse nested paths without separators, but underscores make ambiguous cases easier to read.
Both forms are commonly seen. The underscore version becomes especially helpful when property names can be parsed in more than one way or when the method name is already long.
Add More Conditions Carefully
You can combine nested-property filters with other derived-query keywords.
This is convenient for ordinary queries, but method names can become unreadable if you keep adding conditions, sorting, ranges, and null checks. Once the method name starts feeling like a sentence parser, the design has crossed the line where explicit JPQL or specifications are usually better.
Use JPQL When Semantics Need More Control
Derived queries are great until the query needs custom join behavior, null logic, or fetch strategy.
An explicit query is not a failure. It is the right tool when the derived method name stops being the clearest representation of the actual query.
Watch for Fetching and Performance Issues
A method name can be correct and still perform badly. If later code serializes or maps nested associations, lazy loading can trigger extra queries. In read-heavy paths, a fetch join or a projection may be more appropriate than a simple derived method.
Repository design should be driven by both correctness and the data access pattern. A neat method name does not guarantee efficient SQL.
Integration Tests Matter Here
Repository tests are worth the cost for nested-property queries because they confirm both parsing and actual database behavior.
Mock-only tests cannot prove that Spring parsed the path the way you intended.
Common Pitfalls
- Writing repository method paths from database column names instead of Java property names.
- Letting derived method names grow so long that the query intent becomes unclear.
- Assuming nested-property queries automatically solve lazy-loading or fetch-performance issues.
- Skipping explicit JPQL when the query needs custom join or null-handling semantics.
- Relying only on mocks instead of integration tests for repository behavior.
Summary
- Spring Data can derive queries from nested entity properties such as
customer.email. - Repository method names must follow Java property paths, not database column names.
- Underscores can improve readability for nested paths.
- Derived queries work best for short, clear lookups.
- Switch to JPQL or specifications when the query logic or performance needs more control.
Related reading
- Spring Data JPA - could not initialize proxy - no Session - With Methods marked as transactional
- Spring Data JPA - No Property Found for Type Exception
- Spring Data JPA How to use Kotlin nulls instead of Optional
- Spring Data JPA Unable to locate Attribute with the given name
- Spring Data JPA without Spring Boot
- Spring Data rollback transaction on retry
- Spring data with cassandra giving IllegalStateException
- Spring embeddeb db table already exists error

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.