Databases · SQL Technologies
Hibernate — Free Learning Resources
Free, printable resources for Hibernate — practice problems, quick-reference cheatsheet, and an interview prep sheet. No sign-up required.
Hibernate — Practice Worksheet
Structured exercises and problems to build hands-on Hibernate skills. Work through key concepts step by step.
Hibernate — Cheatsheet
One-page quick-reference for Hibernate — key syntax, commands, patterns, and best practices at a glance.
Hibernate — Interview Sheet
Top Hibernate interview questions with concise answers. Get ready for any technical round with this focused prep sheet.
About Hibernate
Hibernate is the leading Java ORM framework that maps Java classes to database tables, letting developers interact with relational databases using object-oriented code instead of SQL. As the reference implementation of JPA, Hibernate handles caching (L1 and L2), lazy loading, cascade operations, and schema generation — and is used in the majority of Spring Boot applications.
HibernateCheat Sheet — What's Covered
- ✓Entity mapping — @Entity, @Table, @Column, @Id, @GeneratedValue
- ✓Association mapping — @OneToMany, @ManyToOne, @ManyToMany, fetch types
- ✓HQL and Criteria API for type-safe, database-agnostic queries
- ✓First-level and second-level caching with Ehcache or Redis
- ✓N+1 detection, FetchType strategies, and JOIN FETCH solutions
Frequently Asked Questions — Hibernate
What is the difference between Hibernate and JPA?
JPA is a specification defining annotations and APIs for ORM in Java. Hibernate is the most widely used JPA implementation — it provides the actual SQL generation, caching, and database dialect support. You code to the JPA API; Hibernate is the runtime engine.
What are the Hibernate entity lifecycle states?
Transient: new object, not in any session. Persistent: associated with an open session, changes tracked. Detached: previously persistent, session closed, changes not tracked. Removed: marked for deletion on next flush. Transitions via persist(), merge(), remove().
How does Hibernate's first-level cache work?
The L1 cache is the Session cache — mandatory and always active. Within one Session, loading the same entity ID twice returns the cached instance without a second SQL query. The cache is cleared when the Session closes or via session.evict(entity).
What causes the N+1 query problem?
Loading N parent entities with LAZY fetch on a collection, then accessing each collection, fires 1 parent query + N child queries. Fix with JOIN FETCH in HQL: SELECT p FROM Post p JOIN FETCH p.comments, or use @EntityGraph or @BatchSize to batch the lazy loads.
What is the difference between merge() and persist()?
persist() makes a transient entity managed — fails if the entity already has a database ID. merge() copies a detached entity's state into a new managed instance — safe when you don't know if the entity is new. Use merge() in web apps where entities cross request boundaries.
Who Is This For?
Java developers working with relational databases who want to avoid JDBC boilerplate and prefer working with entity objects instead of SQL result sets.