💾 Section 16 · 30 questions

Hibernate JPA

30 interview questions and answers in the Hibernate JPA section.

Questions in this section

  1. What is the N+1 Problem and How to Solve It
  2. What is the Difference Between Lazy and Eager Loading
  3. When to Use Lazy vs Eager Loading
  4. What is LazyInitializationException and How to Avoid It
  5. What Fetch Strategies Exist in Hibernate
  6. What Does the @BatchSize Annotation Do
  7. Describe the Entity Lifecycle in Hibernate
  8. What Are the Transient, Persistent, Detached, Removed States
  9. What is the First-Level Cache in Hibernate
  10. What is the Second-Level Cache and When to Use It
  11. How to Configure the Second-Level Cache
  12. What is Dirty Checking in Hibernate
  13. How Does the Flush Mechanism Work in Hibernate
  14. What is the Difference Between persist() and merge()
  15. What Does the refresh() Method Do
  16. What is EntityManager and How Does It Differ from Session
  17. How to Implement Optimistic Locking in JPA
  18. How to Implement Pessimistic Locking in JPA
  19. What is @Version and Why is It Needed
  20. How Do Cascade Operations Work
  21. What Cascade Types Exist
  22. What is Orphan Removal
  23. How to Properly Use @OneToMany and @ManyToOne
  24. What Are the Peculiarities of Bidirectional Relationships
  25. How to Avoid Infinite Recursion When Serializing Entities
  26. What is JPQL and How Does It Differ from SQL
  27. What is Criteria API and When to Use It
  28. How to Use JOIN FETCH to Solve the N+1 Problem
  29. What is Projection in JPA
  30. What Inheritance Types Does JPA Support

Study navigator

30 essential questions and deep-dive architectural analyses for Junior 🟢, Middle 🟡, and Senior 🔴 Java & Database Persistence Engineers.


📋 Table of Contents

# Question Difficulty
01 What is the N+1 Problem and How to Solve It ⭐⭐
02 What is the Difference Between Lazy and Eager Loading ⭐
03 When to Use Lazy vs Eager Loading ⭐⭐
04 What is LazyInitializationException and How to Avoid It ⭐⭐
05 What Fetch Strategies Exist in Hibernate ⭐⭐⭐
06 What Does the @BatchSize Annotation Do ⭐⭐
07 Describe the Entity Lifecycle in Hibernate ⭐⭐
08 What Are the Transient, Persistent, Detached, Removed States ⭐⭐
09 What is the First-Level Cache in Hibernate ⭐⭐
10 What is the Second-Level Cache and When to Use It ⭐⭐⭐
11 How to Configure the Second-Level Cache ⭐⭐⭐
12 What is Dirty Checking in Hibernate ⭐⭐
13 How Does the Flush Mechanism Work in Hibernate ⭐⭐⭐
14 What is the Difference Between persist() and merge() ⭐⭐
15 What Does the refresh() Method Do ⭐
16 What is EntityManager and How Does It Differ from Session ⭐⭐
17 How to Implement Optimistic Locking in JPA ⭐⭐
18 How to Implement Pessimistic Locking in JPA ⭐⭐
19 What is @Version and Why is It Needed ⭐⭐
20 How Do Cascade Operations Work ⭐⭐
21 What Cascade Types Exist ⭐⭐
22 What is Orphan Removal ⭐⭐
23 How to Properly Use @OneToMany and @ManyToOne ⭐⭐
24 What Are the Peculiarities of Bidirectional Relationships ⭐⭐
25 How to Avoid Infinite Recursion When Serializing Entities ⭐⭐
26 What is JPQL and How Does It Differ from SQL ⭐⭐
27 What is Criteria API and When to Use It ⭐⭐
28 How to Use JOIN FETCH to Solve the N+1 Problem ⭐⭐
29 What is Projection in JPA ⭐⭐
30 What Inheritance Types Does JPA Support ⭐⭐

🗺️ Topic Dependency Map

                    ┌──────────────────────────────────────────┐
                    │      ENTITY LIFECYCLE & CONTEXT (07-08)  │
                    │   07. Lifecycle Graph (New/Managed/etc.) │
                    │   08. State Transitions & PersistenceCtx │
                    │   16. EntityManager vs Hibernate Session │
                    └──────────────────┬───────────────────────┘
                                       │
            ┌──────────────────────────┼──────────────────────────┐
            ▼                          ▼                          ▼
    ┌───────────────┐        ┌───────────────┐        ┌────────────────────┐
    │ FETCHING &    │        │ CACHING &     │        │ LOCKING, CASCADES  │
    │ QUERY OPT     │        │ FLUSHING      │        │ & RELATIONS        │
    │ (01-06, 28)   │        │ (09-15)       │        │ (17-24)            │
    │ 01. N+1 Issue │        │ 09. L1 Cache  │        │ 17. Optimistic Lock│
    │ 02-03. Lazy/Ea│        │ 10-11. L2 Cach│        │ 18. Pessimistic Lk │
    │ 04. LazyInitEx│        │ 12. DirtyCheck│        │ 19. @Version       │
    │ 05. Strategies│        │ 13. Flush Mech│        │ 20-21. Cascades    │
    │ 06. @BatchSize│        │ 14. Persist/Mr│        │ 22. Orphan Removal │
    │ 28. JOIN FETCH│        │ 15. Refresh() │        │ 23-24. Mappings    │
    └───────┬───────┘        └───────┬───────┘        └────────┬───────────┘
            │                        │                        │
            └────────────────────────┼────────────────────────┘
                                     ▼
                    ┌──────────────────────────────────────────┐
                    │    QUERIES, PROJECTIONS & MODELS (25-30) │
                    │    25. Jackson JSON Serialization Loops  │
                    │    26. JPQL vs Native SQL                │
                    │    27. Type-Safe Criteria API            │
                    │    29. DTO & Interface Projections       │
                    │    30. Inheritance (Single/Joined/Table) │
                    └──────────────────────────────────────────┘

🟢 Junior Track (JPA Foundations & Lifecycle)

🟡 Middle Track (Queries, N+1 Prevention & Concurrency)

🔴 Senior Track (High-Throughput Optimization & Second-Level Caches)