💾 Section 16 · 30 questions
Hibernate JPA
30 interview questions and answers in the Hibernate JPA section.
Questions in this section
- What is the N+1 Problem and How to Solve It
- What is the Difference Between Lazy and Eager Loading
- When to Use Lazy vs Eager Loading
- What is LazyInitializationException and How to Avoid It
- What Fetch Strategies Exist in Hibernate
- What Does the @BatchSize Annotation Do
- Describe the Entity Lifecycle in Hibernate
- What Are the Transient, Persistent, Detached, Removed States
- What is the First-Level Cache in Hibernate
- What is the Second-Level Cache and When to Use It
- How to Configure the Second-Level Cache
- What is Dirty Checking in Hibernate
- How Does the Flush Mechanism Work in Hibernate
- What is the Difference Between persist() and merge()
- What Does the refresh() Method Do
- What is EntityManager and How Does It Differ from Session
- How to Implement Optimistic Locking in JPA
- How to Implement Pessimistic Locking in JPA
- What is @Version and Why is It Needed
- How Do Cascade Operations Work
- What Cascade Types Exist
- What is Orphan Removal
- How to Properly Use @OneToMany and @ManyToOne
- What Are the Peculiarities of Bidirectional Relationships
- How to Avoid Infinite Recursion When Serializing Entities
- What is JPQL and How Does It Differ from SQL
- What is Criteria API and When to Use It
- How to Use JOIN FETCH to Solve the N+1 Problem
- What is Projection in JPA
- 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
🗺️ 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) │
└──────────────────────────────────────────┘
🎯 Recommended Study Tracks
🟢 Junior Track (JPA Foundations & Lifecycle)
- Step 1: Entity States & Persistence — 07. Describe the Entity Lifecycle in Hibernate, 08. What Are the Transient, Persistent, Detached, Removed States, 14. What is the Difference Between persist() and merge()
- Step 2: Basic Relationships & Cascades — 20. How Do Cascade Operations Work, 21. What Cascade Types Exist, 23. How to Properly Use @OneToMany and @ManyToOne
- Step 3: Loading Basics — 02. What is the Difference Between Lazy and Eager Loading, 04. What is LazyInitializationException and How to Avoid It
🟡 Middle Track (Queries, N+1 Prevention & Concurrency)
- Step 1: N+1 Elimination & Fetch Strategies — 01. What is the N+1 Problem and How to Solve It, 03. When to Use Lazy vs Eager Loading, 06. What Does the @BatchSize Annotation Do, 28. How to Use JOIN FETCH to Solve the N+1 Problem
- Step 2: Dirty Checking, Caches & Flushing — 09. What is the First-Level Cache in Hibernate, 12. What is Dirty Checking in Hibernate, 13. How Does the Flush Mechanism Work in Hibernate
- Step 3: Locking & Consistency — 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
- Step 4: Mapping & Serialization — 22. What is Orphan Removal, 24. What Are the Peculiarities of Bidirectional Relationships, 25. How to Avoid Infinite Recursion When Serializing Entities, 29. What is Projection in JPA
🔴 Senior Track (High-Throughput Optimization & Second-Level Caches)
- Step 1: Advanced Fetching & Execution Plans — 05. What Fetch Strategies Exist in Hibernate, 26. What is JPQL and How Does It Differ from SQL, 27. What is Criteria API and When to Use It
- Step 2: Second-Level & Query Cache Architectures — 10. What is the Second-Level Cache and When to Use It, 11. How to Configure the Second-Level Cache
- Step 3: Relational Inheritance Strategies — 30. What Inheritance Types Does JPA Support