Top 20 Spring Data JPA Interview Questions and Answers
Top 20 Spring Data JPA Interview Questions and Answers Work through these 20 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.
How do Flyway/Liquibase integrate with Spring Boot JPA?
Prod: spring.jpa.hibernate.ddl-auto=validate, Flyway V1__init.sql through Vn. CI: @Testcontainers PostgreSQL + Flyway migrate + integration tests. Never rename column without...
Best practices for Spring Data pagination with Pageable?
Controller: @PageableDefault(size=20, sort="createdAt", direction=DESC) Pageable p. Cap size at 100. Admin reports: keyset pagination on id > lastId for deep pages — OFFSET...
What is Hibernate second-level cache and when enable it?
Enable for ProductCategory, Currency — READ_ONLY, rarely changed. spring.jpa.properties.hibernate.cache.use_second_level_cache=true with Caffeine region factory. Avoid on...
How implement multi-tenancy in Spring Security context?
JWT includes tenant_id — converter sets TenantContextHolder. Base entity @Column tenantId validated on save. Repository methods always include tenant predicate — or Hibernate...
Saga pattern — choreography vs orchestration?
Order saga: reserve inventory → charge payment → ship. Failure at payment triggers release inventory compensation. Orchestration clearer for complex flows with visibility....
Explain the transactional outbox pattern.
OrderService @Transactional saves Order and OutboxEvent(type=ORDER_CREATED, payload=json). OutboxProcessor @Scheduled polls FOR UPDATE SKIP LOCKED, sends to Kafka, updates...
What do Java Records provide and their limitations?
DTO: record UserResponse(String id, String name) {} — JSON mapping works. Service layer immutable value objects. Not for entities needing setters/lazy proxies yet. Combine with...
What is the N+1 query problem and how do you fix it?
Loading 100 orders then accessing each order's items triggers 101 queries. Fix with JOIN FETCH in JPQL, @EntityGraph on repository method, or a DTO query selecting only...
How does @Transactional work?
Spring creates a proxy around @Transactional beans. Calling createOrder() from another bean starts a transaction on the thread-bound EntityManager; commit on success, rollback...
What are Spring Boot starters?
spring-boot-starter-web brings spring-webmvc, Tomcat, Jackson with tested versions and enables WebMvcAutoConfiguration. You declare one starter instead of 10 coordinates....
How do you handle exceptions globally in Spring?
Create @RestControllerAdvice with @ExceptionHandler(MethodArgumentNotValidException.class) returning 400 + field map, NotFoundException → 404, generic Exception → 500 without...
What is the difference between JPA and Hibernate?
JPA is the API contract; Hibernate is the engine under Spring Boot by default. You write @Entity per JPA spec; Hibernate handles SQL generation. Spring Data JpaRepository adds...
How do you test Spring Boot applications?
Unit test services with Mockito — no Spring. @WebMvcTest + MockMvc for controller contract. @DataJpaTest for repository queries. @SpringBootTest + Testcontainers for critical...
IdentityHashMap vs HashMap — when would you use IdentityHashMap?
Use when identity matters not value — tracking object visitation in deep graph walk. Business keys (userId, email) belong in HashMap with proper equals/hashCode....
What changed migrating Spring Boot 2 to Spring Boot 3 (Jakarta EE)?
Upgrade to Java 17+, replace imports javax.persistence → jakarta.persistence, update Spring Security config to SecurityFilterChain lambda DSL, run OpenRewrite migration recipe,...
How do Spring application events work with @EventListener?
OrderService publishes OrderCreatedEvent after save. EmailListener @EventListener sends confirmation. @TransactionalEventListener(AFTER_COMMIT) prevents email on rolled-back...
Lazy vs eager fetching in JPA — trade-offs?
Default associations LAZY. Service method needing orders+items uses JOIN FETCH in repository query. Disable spring.jpa.open-in-view=false — forces explicit fetch in service...
When use @Query native SQL vs JPQL in Spring Data JPA?
JPQL for standard entity fetches and portable logic. Native for PostgreSQL-specific jsonb queries, recursive CTEs, or performance-tuned index hints after EXPLAIN. Interface...
How do JPA Specifications (Criteria API) help dynamic queries?
UserSearchSpec.withStatus(status).and(withNameLike(name)) — repository.findAll(spec, pageable). Criteria API verbose — wrap in static factory methods. Alternative: Querydsl or...
What is JPA auditing with @CreatedDate and @LastModifiedDate?
@EnableJpaAuditing + AuditorAware<String> from SecurityContextHolder. Entity: @CreatedDate Instant createdAt; @LastModifiedDate Instant updatedAt. Works with auditing on save()...
Spring Data JPA Interview Questions and Answers · Top 50 Spring Data JPA Interview Questions and Answers (2026 Edition) · Top 40 Spring Data JPA Interview Questions | Must-Prepare before interview. · Spring Data JPA Interview Questions Every Fresher Must Know
All interview question lists · Interview guides · Home · Support the platform