Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 39 ยท Getting Started

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.

Almost every business application keeps its data in a relational database and its logic in objects. Tables, rows and foreign keys on one side; classes, fields and references on the other. Hibernate ORM is a Java library that sits between the two. You describe how your classes map to tables, and Hibernate generates the SQL, runs it through JDBC, and turns result rows back into objects.

This lesson explains the problem Hibernate solves, what it does for you, where it can hurt, and how this course handles the fact that the real framework cannot run inside a web page.

How the examples work. Hibernate needs a build tool, its own dependencies and a JVM with those jars. The runnable editors on this site have the JDK and an in-memory H2 database, but not Hibernate itself. So every real Hibernate snippet in this course is read-only, and every runnable program is a clearly labelled stand-in: plain Java and JDBC that reproduces the idea (mapping by annotations, dirty checking, caching, locking) so you can see the mechanism. We only show Hibernate output when we are certain about it.

The object-relational mismatch

Objects and tables do not line up neatly. The usual friction points are:

  • Identity. Two Java objects are the same if == says so; two rows are the same if their primary keys match.
  • Graphs vs. joins. An Order holds a List<OrderLine>; in SQL the lines hold a foreign key back to the order.
  • Inheritance. SQL has no extends. You must choose how a class hierarchy lands in tables.
  • Types. LocalDate, enums and value objects need a column representation, and NULL has no Java twin.
  • Navigation. In Java you call order.getCustomer().getName(); in SQL you decide up front which joins to run.

Writing the translation by hand for every class is repetitive and easy to get wrong. That is the "mapping" in ORM.

What an ORM does

Objects Classes, fields, references
Mapping Annotations describe the link
Tables Rows, columns, foreign keys

Hibernate generates the SQL for you.

1 / 3

The manual way (JDBC)

Here is what mapping looks like without an ORM. This is a runnable plain-JDBC program, not Hibernate: it saves one book and loads it back, with every column handled by hand.

Java Manual mapping with JDBC

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

For one three-column class that is fine. For fifty classes with relations, it is thousands of lines that all say the same thing.

The ORM way: describe, don't write

With Hibernate you describe the mapping once, with annotations, and use the object API. This snippet is the real thing (read-only here):

Java
@Entity
@Table(name = "book")
public class Book {
    @Id
    private Long id;
    private String title;
    private int published;

    protected Book() {}                       // Hibernate needs a no-arg constructor
    public Book(Long id, String title, int published) { ... }
}

// somewhere in your code
session.persist(new Book(1L, "Refactoring", 2018));   // INSERT
Book book = session.find(Book.class, 1L);             // SELECT

Two calls replace all the PreparedStatement code. The next runnable program is a stand-in that shows the mechanism: an annotation on a record, and a small piece of reflection that generates the table and the SQL. Real Hibernate does the same kind of metadata reading, with far more features and a lot of caching.

Java Stand-in: a 40-line mapper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

What Hibernate adds beyond mapping

A real ORM is much more than SQL generation:

  • A persistence context that tracks every loaded object and saves changes automatically (dirty checking).
  • Lazy loading of relationships so you only pay for data you touch.
  • Caches at several levels, and batching of statements.
  • A query language (HQL) and a type-safe Criteria API.
  • Dialects that adapt SQL to PostgreSQL, MySQL, Oracle, SQL Server and others.
  • Transactions and locking support, including optimistic versioning.

When to use it, and when not to

Hibernate fits applications with a rich domain model and mostly record-at-a-time operations: back offices, e-commerce, CRUD-heavy services. It is a weaker fit for heavy reporting, bulk analytics or very SQL-centric code, where plain SQL, JDBC or a SQL mapper is simpler. Many teams use both: Hibernate for writes and the domain model, native SQL for reports. Hibernate also hides SQL, which makes it easy to produce slow queries without noticing. A recurring theme of this course is learning to see the SQL it generates.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem does an ORM mainly solve?

    An ORM maps classes to tables and rows back to objects, so you do not write that translation by hand.
  2. 2. In this course, why are real Hibernate snippets read-only?

    The runner has the JDK and H2 but not Hibernate, so runnable code is a labelled stand-in built from plain Java and JDBC.
  3. 3. Which is NOT one of the object-relational mismatches?

    Strings map to VARCHAR/TEXT columns without trouble. The other three are classic mismatches.
  4. 4. A downside of an ORM mentioned in this lesson is:

    Because the SQL is generated, you must learn to inspect it to avoid, for example, too many queries.

Summary

  • Hibernate ORM maps Java classes to relational tables and generates the SQL for you.
  • It exists because objects and rows differ in identity, relationships, inheritance and types.
  • Beyond mapping it offers change tracking, lazy loading, caching, a query language and dialects.
  • Its cost is hidden SQL and a learning curve: states, flushing and fetching.
  • Here, real Hibernate code is read-only; runnable programs are plain-Java stand-ins.

Next: Hibernate, JPA and the Architecture