Skip to content
Tutorials Center
My learning

Topics

Hibernate ORM Tutorial

Learn Hibernate ORM step by step: 39 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Is Hibernate ORM? Learn what Hibernate ORM is, which problem object-relational mapping solves, what you gain and lose compared with plain JDBC, and how this course runs its examples.
  2. 2 Hibernate, JPA and the Architecture Understand how Hibernate relates to Jakarta Persistence (JPA), the layers between your code and the database, and the key interfaces: SessionFactory, Session and Transaction.
  3. 3 Your First Hibernate Project Set up a first Hibernate 7 project step by step with Maven, a hibernate.cfg.xml file, an entity class and a main program, and see the project structure and bootstrap flow.
  4. 4 CRUD with Hibernate Create, read, update and delete entities with Hibernate Session methods persist, find, merge and remove, see automatic updates, and learn the SQL behind each operation.

Configuration and Sessions

  1. 5 Hibernate Configuration Configure Hibernate with hibernate.cfg.xml, persistence.xml, properties and code. Learn the key settings, connection pooling, batching and how to log SQL and bind parameters.
  2. 6 SessionFactory and Session Learn how Hibernate's SessionFactory and Session differ, why one is shared and the other is short-lived, session-per-request, closing sessions and the StatelessSession.
  3. 7 Entity States and the Persistence Context Understand the four Hibernate entity states, the persistence context as first-level cache, how persist, merge, detach, clear and refresh move objects between states.
  4. 8 Flush and Dirty Checking Learn when Hibernate sends SQL, how dirty checking finds changed entities, the flush modes AUTO, COMMIT and MANUAL, the order of statements and how to tune updates.
  5. 9 Schema Generation and Naming Strategies Learn what hibernate.hbm2ddl.auto and Jakarta schema generation do, why update is risky in production, how migrations help, and how naming strategies map names to snake_case.
  6. 10 Bootstrapping Hibernate in Code Build a Hibernate SessionFactory in plain Java: StandardServiceRegistry, MetadataSources, Metadata, the bootstrap steps, and a runnable stand-in of the builder flow.

Entity Mapping

  1. 11 Entity and Column Mapping Map a Java class to a table with @Entity, @Table, @Id, @Column, @Transient and @Access. Learn the entity rules, column options and a runnable mapping stand-in.
  2. 12 Identifiers and Generation Strategies Choose primary keys in Hibernate: @Id, @GeneratedValue with IDENTITY, SEQUENCE, UUID, composite keys with @EmbeddedId, and why SEQUENCE enables JDBC batching.
  3. 13 Types, Enums and Converters Map Java types to columns in Hibernate: strings, numbers, java.time dates, enums with @Enumerated, booleans, and custom AttributeConverter classes with runnable stand-ins.
  4. 14 Embeddables and Value Objects Group columns into value objects with @Embeddable and @Embedded, override column names with @AttributeOverride, reuse an address twice and flatten rows in a runnable stand-in.
  5. 15 Element Collections and Maps Store collections of basic values or embeddables with @ElementCollection, @CollectionTable, @OrderColumn and @MapKeyColumn, and see the SQL tables behind them.

Associations and Inheritance

  1. 16 Many-to-One and One-to-Many Map the most common association: @ManyToOne with a foreign key, bidirectional @OneToMany with mappedBy, helper methods that keep both sides in sync, and the join-column pitfalls.
  2. 17 One-to-One Mapping Map one-to-one associations in Hibernate: a foreign key with @JoinColumn, the bidirectional mappedBy side, the lazy-loading catch, and the shared primary key with @MapsId.
  3. 18 Many-to-Many Mapping Map many-to-many in Hibernate with @ManyToMany and @JoinTable, choose Set over List, keep both sides in sync, and learn when a link entity with extra columns is better.
  4. 19 Cascade and Orphan Removal Understand Hibernate cascading: PERSIST, MERGE, REMOVE, ALL, orphanRemoval, when to cascade from a parent to its children, and when cascading is dangerous.
  5. 20 Inheritance Mapping Strategies Map Java class hierarchies to tables in Hibernate: @MappedSuperclass, SINGLE_TABLE with a discriminator, JOINED, TABLE_PER_CLASS, polymorphic queries and how to choose.

HQL, Criteria and Native Queries

  1. 21 HQL Basics: Querying Entities, Not Tables Learn HQL, Hibernate's object query language - select entities, filter with named parameters, sort, page and run typed queries, with a runnable HQL-to-SQL translator stand-in.
  2. 22 HQL Joins, Aggregates and Subqueries Join entities in HQL with inner, left and fetch joins, group and filter with aggregates, write subqueries and case expressions, and run the equivalent SQL on H2.
  3. 23 The Criteria API: Building Queries in Java Build queries with the JPA Criteria API and Hibernate's HibernateCriteriaBuilder - predicates, dynamic filters, the metamodel and when to prefer it over HQL, with a runnable builder stand-in.
  4. 24 Native SQL, Named Queries and Projections Run native SQL from Hibernate, map results to entities and records, declare named queries, and page efficiently with offset and keyset pagination - with runnable H2 stand-ins.
  5. 25 HQL Functions, CASE Expressions and Bulk Operations Use HQL functions, CASE, COALESCE and date expressions, then run bulk update, delete and insert statements and learn why they leave the persistence context stale.

Caching and Performance

  1. 26 The Session Cache, Dirty Checking and Flushing Understand Hibernate's first-level cache - the persistence context, identity guarantee, dirty checking snapshots, flush modes and when SQL really runs, with a runnable mini-session.
  2. 27 N+1 Queries and Fetch Strategies Find and fix Hibernate's N+1 select problem with lazy loading, fetch joins, batch fetching, entity graphs and DTO projections, and count the queries in a runnable demo.
  3. 28 Batch Processing and Hibernate Statistics Insert and update thousands of rows efficiently with JDBC batching, flush-and-clear loops and StatelessSession, and measure queries with Hibernate statistics.
  4. 29 The Second-Level Cache Learn how the Hibernate second-level cache works, how to enable it with JCache providers, choose concurrency strategies, and avoid stale data and wasted memory.
  5. 30 Query Cache and Cache Regions Understand how the Hibernate query cache stores result ids, how timestamps invalidate it, when it helps, and how to size and monitor cache regions.

Transactions and Locking

  1. 31 Transactions and Isolation Levels Learn how Hibernate transactions map to JDBC and JTA, how to commit and roll back safely, and what the isolation levels allow - dirty, non-repeatable and phantom reads.
  2. 32 Optimistic Locking with @Version Prevent lost updates with Hibernate optimistic locking - the @Version column, OptimisticLockException, lock modes, and how to handle conflicts in web apps.
  3. 33 Pessimistic Locking Use Hibernate pessimistic locks - PESSIMISTIC_WRITE, PESSIMISTIC_READ, lock timeouts and SKIP LOCKED - to serialize access to hot rows, and learn when optimistic locking is better.
  4. 34 Retries, Deadlocks and Concurrency Patterns Build safe retry loops for optimistic lock failures and deadlocks, avoid deadlocks with lock ordering, and make operations idempotent in Hibernate applications.

Testing and Best Practices

  1. 35 Testing Hibernate Code Test Hibernate repositories with real databases, Testcontainers, rollback-per-test isolation, query-count assertions and N+1 guards, and know what H2 cannot prove.
  2. 36 Hibernate Best Practices and Anti-Patterns A checklist of Hibernate best practices - lazy associations, equals and hashCode, DTO projections, open-in-view, batching and schema control - and the anti-patterns to avoid.

Projects and Practice

  1. 37 Project: A Library Repository Layer Build a small library system - entities, a repository layer, a fetch-aware query and a statement-count check - with Hibernate code and a runnable JDBC stand-in.
  2. 38 Project: Money Transfers with Locking and Retries Build a money-transfer service step by step - invariants, optimistic versions, ordered pessimistic locks, retries and an audit ledger - and test it with concurrent threads.
  3. 39 Hibernate Practice Questions and Debugging Drills Test what you learned with Hibernate practice questions, debugging drills for common exceptions, a query-tuning exercise and interview-style answers.