Java Interview Preparation
Spring Data JPA and Hibernate Interview Questions
Repositories, queries, fetching, caching, locking, transactions, and persistence performance.
Spring Data JPA and Hibernate Interview Focus
Repositories, queries, fetching, caching, locking, transactions, and persistence performance.
Interview questions
1. What is Spring Data JPA? Beginner
Spring Data JPA is an abstraction that creates repository implementations for the JPA standard. It reduces data-access boilerplate while allowing JPQL, specifications, and native queries when needed.
2. How is Spring Data JPA different from Hibernate? Beginner
JPA is a standard API, Hibernate is a popular JPA implementation, and Spring Data JPA is a repository abstraction built on top of JPA. They solve different layers of the persistence problem.
3. What is the difference between CrudRepository and JpaRepository? Beginner
CrudRepository provides basic CRUD operations. JpaRepository adds JPA-oriented methods such as flush and batch operations and also includes paging and sorting through its repository hierarchy.
4. How do derived query methods work? Intermediate
Spring Data parses a repository method name such as findByEmailAndStatus and builds a query from its parts. This is convenient for simple queries; use @Query for complex logic.
5. What is the purpose of @Query? Beginner
@Query lets you declare JPQL or native SQL directly on a repository method. It is useful when a derived method name would be unclear or cannot express the required query.
6. What is the difference between JPQL and native SQL? Intermediate
JPQL uses entity and field names and is portable across databases. Native SQL uses database tables and columns, so it can use vendor features but is less portable.
7. What is the difference between save() and saveAndFlush()? Intermediate
save() registers the entity for persistence but the SQL may wait until flush or transaction commit. saveAndFlush() explicitly flushes the persistence context immediately after saving.
8. What is entity lifecycle management? Intermediate
An entity can be transient, managed, detached, or removed. A managed entity is tracked by the persistence context, so changes can be written automatically during flush.
9. What is the difference between FetchType.LAZY and FetchType.EAGER? Intermediate
LAZY loads a relationship when it is accessed, while EAGER loads it immediately. LAZY is usually safer for performance because it avoids fetching data that the request does not need.
10. What is the N+1 query problem? Advanced
N+1 happens when one query loads parent rows and an additional query runs for each parent. Use fetch joins, EntityGraph, carefully designed DTO queries, or batch fetching, then verify the SQL.
11. How do you solve the N+1 query problem? Advanced
N+1 happens when one query loads parent rows and an additional query runs for each parent. Use fetch joins, EntityGraph, carefully designed DTO queries, or batch fetching, then verify the SQL.
12. What is a fetch join? Intermediate
A fetch join loads a relationship in a JPQL query. EntityGraph declares the fetch plan separately from the query. Both can solve N+1, but the best choice depends on whether the fetch plan is query-specific or reusable.
13. What is the difference between EntityGraph and fetch join? Advanced
A fetch join loads a relationship in a JPQL query. EntityGraph declares the fetch plan separately from the query. Both can solve N+1, but the best choice depends on whether the fetch plan is query-specific or reusable.
14. What is the first-level cache? Intermediate
The first-level cache belongs to one persistence context or EntityManager. Within a transaction, repeated requests for the same entity can reuse the managed instance without another database query.
15. What is the second-level cache? Intermediate
The second-level cache is shared by sessions through a configured cache provider. It can reduce database reads across transactions, but invalidation and stale-data rules must be understood.
16. What is dirty checking in Hibernate? Intermediate
Hibernate keeps a snapshot of managed entities. During flush it compares the current state with that snapshot and generates update SQL only when a tracked value changed.
17. What is cascading? Beginner
Cascading propagates an entity operation such as persist, merge, or remove from a parent to related entities. Use only the cascade types that match the ownership and lifecycle rules.
18. What is orphan removal? Intermediate
orphanRemoval deletes a child that is removed from the parent relationship. CascadeType.REMOVE deletes children when the parent is deleted. They are related but are triggered by different actions.
19. What is the difference between CascadeType.REMOVE and orphanRemoval? Advanced
orphanRemoval deletes a child that is removed from the parent relationship. CascadeType.REMOVE deletes children when the parent is deleted. They are related but are triggered by different actions.
20. What is optimistic locking? Advanced
Optimistic locking assumes conflicts are uncommon. A @Version column changes on each update; if another transaction changed the row first, the update affects zero rows and Hibernate reports an optimistic-lock exception.
21. What is pessimistic locking? Advanced
Pessimistic locking asks the database to lock selected rows while a transaction works with them. It can prevent concurrent changes but may reduce throughput and cause blocking.
22. What is the purpose of @Version? Intermediate
Optimistic locking assumes conflicts are uncommon. A @Version column changes on each update; if another transaction changed the row first, the update affects zero rows and Hibernate reports an optimistic-lock exception.
23. How do you implement pagination using Spring Data JPA? Intermediate
Accept a Pageable parameter and return Page<T> or Slice<T> from the repository. Add a stable sort, enforce reasonable page sizes, and remember that large offsets can become expensive.
24. How do you perform batch inserts and updates? Advanced
Use JDBC batching settings, flush and clear in batches, avoid unnecessary entity loading, inspect generated SQL, and use database execution plans. Measure before and after the change.
25. How do you troubleshoot slow database queries? Advanced
Use JDBC batching settings, flush and clear in batches, avoid unnecessary entity loading, inspect generated SQL, and use database execution plans. Measure before and after the change.
How to Prepare for Java Technical Interviews
Use this Java technical interview question bank to revise core concepts and practise explaining your decisions clearly. The questions cover Java fundamentals, object-oriented programming, collections, exceptions, multithreading, Spring Boot, Hibernate, databases, and other topics used in real software development interviews.
Start with the fundamentals, then move to scenario-based questions and advanced topics. Filter questions by difficulty to build confidence gradually. A strong answer should define the concept, explain why it matters, and include a practical example when appropriate.
Continue your preparation with the Java tutorials, or explore all interview preparation tracks for managerial and company-focused questions.
