Spring Data JPA Repositories
Give a real production-style example of using Spring Data JPA Repositories. Walk through the scenario end-to-end.
Answers use simple, clear English.
Audio N/AQuick interview answer
Scenario: findByEmailIgnoreCaseAndActiveTrue(String email) generates query at startup. Implementation notes: Use @EntityGraph or JOIN FETCH for associations; DTO projections for reads.
Detailed answer
Scenario: findByEmailIgnoreCaseAndActiveTrue(String email) generates query at startup. Implementation notes: Use @EntityGraph or JOIN FETCH for associations; DTO projections for reads. Core: Interface extending JpaRepository<T,ID> gets CRUD + query methods from method names. @Query for JPQL/native; Pageable for pagination. Real-time example: findByEmailIgnoreCaseAndActiveTrue(String email) generates query at startup. Pros: Minimal boilerplate; pagination and sorting built-in. Cons: Derived query names explode; N+1 if fetch graphs missing. Common mistakes: Long derived method names unreadable; missing @Transactional on service layer writes. Best practices: Use @EntityGraph or JOIN FETCH for associations; DTO projections for reads. Audience level: Architect.
Full explanation
Interface extending JpaRepository<T,ID> gets CRUD + query methods from method names. @Query for JPQL/native; Pageable for pagination.
Real example & use case
findByEmailIgnoreCaseAndActiveTrue(String email) generates query at startup.
Pros & cons
Pros: Minimal boilerplate; pagination and sorting built-in. Cons: Derived query names explode; N+1 if fetch graphs missing.
Common mistakes
Long derived method names unreadable; missing @Transactional on service layer writes.
Best practices
Use @EntityGraph or JOIN FETCH for associations; DTO projections for reads.
Follow-up questions
Open one as its own read / solve / listen card