Spring Data JPA Repositories
You are a Junior on-call. A production issue might involve Spring Data JPA Repositories. How do you diagnose and mitigate?
Answers use simple, clear English.
Audio N/AQuick interview answer
Mitigate first, then root-cause. Check symptoms against: Long derived method names unreadable; missing @Transactional on service layer writes..
Detailed answer
Mitigate first, then root-cause. Check symptoms against: Long derived method names unreadable; missing @Transactional on service layer writes.. Validate with: Use @EntityGraph or JOIN FETCH for associations; DTO projections for reads.. Context: Interface extending JpaRepository<T,ID> gets CRUD + query methods from method names. @Query for JPQL/native; Pageable for pagination. 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: Junior.
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