LearnThatStack Ace your next interview
Backend Development

Hibernate.

Start free Change topic Change
Practice · Questions

All questions

Showing of 42
Beginner 9
01

What is the difference between Hibernate and JDBC?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

JDBC makes you write the SQL and map results by hand, while Hibernate maps objects to tables and writes the SQL for you. JDBC is Java's low-level API for running SQL. Hibernate is an object-relational mapping (ORM) framework built on top of it.

Aspect JDBC Hibernate
Code complexity More boilerplate code Minimal code required
Database dependency Database-specific SQL Database-independent HQL
Object mapping Manual conversion Automatic ORM
Caching No built-in caching Multi-level caching
Performance Faster for simple operations Better for complex applications
Learning curve Easier to learn Steeper learning curve

JDBC gives you full control over each statement and has no mapping overhead, so it suits small tools and hand-tuned queries. Hibernate saves the most work in an application with a large domain model and many related entities.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

02

What are the core interfaces of Hibernate?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

The core types are Configuration, SessionFactory and Session. Transaction, Query and CriteriaBuilder complete the set.

  1. Configuration: a class, not an interface. It reads the settings and creates the SessionFactory.
  2. SessionFactory: the factory for Session objects. It is thread-safe and expensive to create.
  3. Session: the main interface for persistence operations. It is not thread-safe.
  4. Transaction: begins, commits and rolls back a database transaction.
  5. Query: runs HQL and native SQL queries.
  6. CriteriaBuilder: builds queries in code. It replaced Hibernate's legacy Criteria API, which Hibernate 6.0 removed.

Session extends JPA's EntityManager, so the same object also serves JPA code.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

03

What is a SessionFactory and how is it different from Session?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A SessionFactory is the heavyweight, thread-safe factory that an application builds once at startup. A Session is the lightweight object it creates for a single unit of work, opened and closed around each transaction.

The SessionFactory reads the configuration once and is immutable after that. It is expensive to create, so an application typically keeps one. It also holds the second-level cache, which every session it creates shares.

The Session is the interface between your Java code and Hibernate. It is inexpensive to create and is not thread-safe, so each request or thread gets its own and closes it when the work is done.

// SessionFactory - created once
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

// Session - created per transaction
Session session = sessionFactory.openSession();
// ... perform operations
session.close();
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

04

What are the different states of a Hibernate entity?

Visual

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain
Visual

An entity starts transient and becomes persistent when a session tracks it. Later it can become detached or removed.

  1. Transient. New, unknown to any session, and with no database row.
  2. Persistent. Also called managed. The session tracks it and saves its changes to its row.
  3. Detached. Its session closed, so changes are no longer tracked.
  4. Removed. Marked for deletion. Hibernate deletes the row at the next flush, when pending changes go to the database.
User user = new User(); // Transient state
session.persist(user);  // Persistent state
session.close();        // Detached state
user = newSession.merge(user); // Persistent again
newSession.remove(user);       // Removed state

The last two lines use a new session, because the first one is closed.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

05

What is the difference between save(), persist(), and saveOrUpdate()?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

save() returns the generated identifier, persist() returns nothing, and saveOrUpdate() either saves a new object or updates an existing one.

  • save() saves the entity and returns its identifier. With an identity column, where the database generates the id, it runs the INSERT at once, even outside a transaction.
  • persist() is the Java Persistence API (JPA) method. It does not promise to assign the identifier at once, and it never runs the INSERT outside a transaction.
  • saveOrUpdate() checks the identifier, then saves a new entity or updates a detached one, an object whose session has closed.
// save() - returns ID
Long id = (Long) session.save(user);

// persist() - no return value
session.persist(user);

// saveOrUpdate() - smart save/update
session.saveOrUpdate(user);

Hibernate 7 removed save() and saveOrUpdate(). Use persist() for a new object and merge() for a detached one.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

06

What are the ways to configure Hibernate?

Part of Pro
07

What is the purpose of @Entity and @Table annotations?

Part of Pro
08

What is the difference between get() and load() in Hibernate?

Part of Pro Visual
09

What is HQL and how is it different from SQL?

Part of Pro
Intermediate 21
10

Explain the difference between lazy and eager loading in Hibernate.

Part of Pro Visual
11

What is the N+1 problem and how can it be solved?

Part of Pro Visual
12

Explain the Hibernate caching mechanism.

Part of Pro Visual
13

What are the different cascade types in Hibernate?

Part of Pro
14

Explain the different inheritance mapping strategies in Hibernate.

Part of Pro
15

What are the different association mappings in Hibernate?

Part of Pro Visual
16

What is the difference between merge() and update()?

Part of Pro Visual
17

What are Hibernate Interceptors and Event Listeners?

Part of Pro
18

Explain the concept of dirty checking in Hibernate.

Part of Pro Visual
19

What causes a LazyInitializationException and how do you fix it?

Part of Pro Visual
20

What is the difference between @Embeddable and @Embedded?

Part of Pro
21

What is the difference between getCurrentSession() and openSession()?

Part of Pro
22

How do you handle optimistic locking in Hibernate?

Part of Pro Visual
23

What are the different transaction isolation levels and their implications?

Part of Pro Visual
24

Explain Hibernate's connection pooling and configuration.

Part of Pro
25

What is the difference between Criteria API and Query by Example?

Part of Pro
26

How do you handle database schema evolution with Hibernate?

Part of Pro
27

Explain the concept of Hibernate Envers for auditing.

Part of Pro
28

How do you handle composite primary keys in Hibernate?

Part of Pro
29

What are Hibernate filters and how are they used?

Part of Pro
30

What are the differences between JPA and Hibernate-specific features?

Part of Pro
Expert 12
31

How would you optimize Hibernate performance for large datasets?

Part of Pro
32

Explain Hibernate's flush modes and when to use each.

Part of Pro
33

How do you implement custom Hibernate types?

Part of Pro
34

What are the best practices for Hibernate session management in web applications?

Part of Pro
35

How do you implement bulk operations efficiently in Hibernate?

Part of Pro Visual
36

What is the difference between stateless and stateful sessions?

Part of Pro Visual
37

Explain Hibernate's second-level cache providers and their characteristics.

Part of Pro
38

How do you implement multi-tenancy in Hibernate?

Part of Pro
39

What are the considerations for Hibernate in microservices architecture?

Part of Pro
40

How do you troubleshoot and debug Hibernate performance issues?

Part of Pro
41

How do you implement custom Hibernate naming strategies?

Part of Pro
42

How should you implement equals() and hashCode() for a Hibernate entity?

Part of Pro Visual

No matches

Try a different filter or search term.

Know someone prepping for Hibernate? Send them this set.
Quick recap

Hibernate in pictures.

A quick recap of Hibernate in 30 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.

Pro The first chapter is free to play. The other 27 pictures come with Pro.

Get Pro · $10/mo
Pro · $10/mo

37 of 42 Hibernate answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime

Change topic

Pick a different technology or stack. Your current topic stays put until you choose a new one.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

Serverless on AWS

Serverless Architecture on AWS

Flutter Mobile

Flutter Cross-Platform Mobile Development

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

AI Engineer

LLMs, RAG, Agents, Evals

AI-Powered Developer

Claude Code, Copilot, Agentic Workflows

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git