All questions
Showing of 42What is the difference between Hibernate and JDBC?
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:
Last attempt -
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.
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 ↓
What are the core interfaces of Hibernate?
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:
Last attempt -
The core types are Configuration, SessionFactory and Session. Transaction, Query and CriteriaBuilder complete the set.
Configuration: a class, not an interface. It reads the settings and creates theSessionFactory.SessionFactory: the factory forSessionobjects. It is thread-safe and expensive to create.Session: the main interface for persistence operations. It is not thread-safe.Transaction: begins, commits and rolls back a database transaction.Query: runs HQL and native SQL queries.CriteriaBuilder: builds queries in code. It replaced Hibernate's legacyCriteriaAPI, which Hibernate 6.0 removed.
Session extends JPA's EntityManager, so the same object also serves JPA code.
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 ↓
What is a SessionFactory and how is it different from Session?
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:
Last attempt -
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();
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 ↓
What are the different states of a Hibernate entity?
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:
Last attempt -
An entity starts transient and becomes persistent when a session tracks it. Later it can become detached or removed.
- Transient. New, unknown to any session, and with no database row.
- Persistent. Also called managed. The session tracks it and saves its changes to its row.
- Detached. Its session closed, so changes are no longer tracked.
- 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.
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 ↓
What is the difference between save(), persist(), and saveOrUpdate()?
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:
Last attempt -
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 theINSERTat 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 theINSERToutside 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.
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 ↓
What are the ways to configure Hibernate?
What is the purpose of @Entity and @Table annotations?
What is the difference between get() and load() in Hibernate?
What is HQL and how is it different from SQL?
Explain the difference between lazy and eager loading in Hibernate.
What is the N+1 problem and how can it be solved?
Explain the Hibernate caching mechanism.
What are the different cascade types in Hibernate?
Explain the different inheritance mapping strategies in Hibernate.
What are the different association mappings in Hibernate?
What is the difference between merge() and update()?
What are Hibernate Interceptors and Event Listeners?
Explain the concept of dirty checking in Hibernate.
What causes a LazyInitializationException and how do you fix it?
What is the difference between @Embeddable and @Embedded?
What is the difference between getCurrentSession() and openSession()?
How do you handle optimistic locking in Hibernate?
What are the different transaction isolation levels and their implications?
Explain Hibernate's connection pooling and configuration.
What is the difference between Criteria API and Query by Example?
How do you handle database schema evolution with Hibernate?
Explain the concept of Hibernate Envers for auditing.
How do you handle composite primary keys in Hibernate?
What are Hibernate filters and how are they used?
What are the differences between JPA and Hibernate-specific features?
How would you optimize Hibernate performance for large datasets?
Explain Hibernate's flush modes and when to use each.
How do you implement custom Hibernate types?
What are the best practices for Hibernate session management in web applications?
How do you implement bulk operations efficiently in Hibernate?
What is the difference between stateless and stateful sessions?
Explain Hibernate's second-level cache providers and their characteristics.
How do you implement multi-tenancy in Hibernate?
What are the considerations for Hibernate in microservices architecture?
How do you troubleshoot and debug Hibernate performance issues?
How do you implement custom Hibernate naming strategies?
How should you implement equals() and hashCode() for a Hibernate entity?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
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.
Hibernate cheatsheet
- Hibernate, JPA & setup01
- Sessions & entity states02
- Map classes to tables03
- Associations & cascade04
- Lazy loading & N+105
- HQL, Criteria & filters06
- First & second-level cache07
- Transactions & locking08
- Bulk work & performance09
- Schema, audit & services10
- The model11
- + 5 more inside
This picture is part of Pro. The first chapter is free to play; the other 27 pictures come with Pro.
Get Pro · $10/mo Already Pro? Sign in37 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.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.