Skip to content
IT glossary
BackendUpdated December 3, 2026

Hibernate

An ORM library that maps Java objects to data in a relational database. It generates and runs SQL to load and save those objects.

Also known as: JPA

What Hibernate does

A Java application might represent an order as an object with a list of line items. A relational database stores that same information as rows in separate tables linked by a foreign key. Hibernate ORM handles the mapping between those representations. It saves you from writing the code that turns rows into objects or copies object fields into SQL statements.

Hibernate is not another name for JPA. Jakarta Persistence, still commonly called JPA, is the specification that defines annotations such as @Entity and the EntityManager API. Hibernate is a provider that implements that specification and also offers its own API, including Session. Spring Data JPA is a separate layer that helps you define repositories on top of a provider like Hibernate.

From a Java object to a SQL statement

Mapping metadata tells Hibernate how to store an entity. For example, @Id marks the entity identifier and @ManyToOne can map an order's reference to its customer. Hibernate generates SQL for the configured database dialect and runs it through JDBC.

The persistence context tracks managed entities. If you call find twice for the same entity ID in the same context, the second call can return the same Java instance without another query. This is the first-level cache, not a cache shared across requests. Hibernate also checks managed objects for changes, a process called dirty checking. A flush synchronizes those changes with the database, typically before commit and sometimes earlier. Flushing does not commit the transaction: the SQL may already have run, but the transaction can still roll back.

Example: update a row without calling save

Assume order 42 exists and this code runs inside a transaction:

Order order = em.find(Order.class, 42L);
order.setStatus(OrderStatus.SHIPPED);

Here em is an EntityManager. Since the order is managed, Hibernate picks up the status change and issues an UPDATE when it flushes. If you close the persistence context and then call the setter, the order is detached. Changing that Java object alone no longer changes the database row.

Where it fits and what to watch for

Hibernate is useful in Java services that routinely read and update related records. You can still use SQL directly for a report or bulk operation when expressing the work in terms of entities would be awkward.

A classic pitfall is the N+1 query problem. A list of orders may take only one query to load, but accessing each order's lazy line-item collection in a loop can trigger one extra query per order. Inspect the SQL and choose a fetch plan for that specific operation. For instance, a JPQL fetch join can load the line items along with the orders. Making every relationship eager can fetch data you don't need and doesn't guarantee a single query.

In interviews

Expect to explain why the setter in the example is enough, when the SQL runs, and why flush and commit are different. You may also be asked to diagnose a LazyInitializationException: for example, a response serializer tries to read an unloaded collection after the persistence context closes. A good answer identifies what the response needs and loads it explicitly before closing the context.