Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 39 ยท Getting Started

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.

People say "Hibernate" and "JPA" almost interchangeably, but they are different things. Jakarta Persistence (JPA) is a specification: a set of annotations and interfaces such as @Entity, EntityManager and EntityManagerFactory. Hibernate ORM is an implementation of that specification, plus a lot of extra features of its own. Understanding the split tells you which imports to use, what is portable, and where the documentation for a feature lives.

This lesson draws the map: the layers from your code down to the database, the main interfaces you will meet in the rest of the course, and the choice between the standard JPA API and Hibernate's native API.

Specification versus implementation

JPA defines the contract: the annotations, the query language (JPQL), the lifecycle rules. Providers implement it. Hibernate is by far the most widely used provider; EclipseLink is another. Application servers and Spring Boot default to Hibernate.

Concept Jakarta Persistence (standard) Hibernate (native)
Factory EntityManagerFactory SessionFactory (extends it)
Unit of work EntityManager Session (extends it)
Transaction EntityTransaction Transaction
Query language JPQL HQL (a superset of JPQL)
Annotations jakarta.persistence.* org.hibernate.annotations.* for extras

In Hibernate 6 and later, SessionFactory extends EntityManagerFactory and Session extends EntityManager. So a Session is an EntityManager: you can code against the standard API and cast down (or unwrap) when you need a Hibernate-only feature.

Java
EntityManager em = emf.createEntityManager();          // standard JPA
Session session = em.unwrap(Session.class);            // Hibernate-specific features

Prefer the standard jakarta.persistence annotations and interfaces by default, and reach for org.hibernate types only when you need something JPA lacks (batch sizes, natural IDs, filters, @Formula, and similar). Keeping the Hibernate-specific code in few places makes it easy to find and to replace.

The layers

Your code never talks to the database directly. A request travels through several layers:

flowchart TD
    A["Your application code<br/>(services, repositories)"] --> B["Session / EntityManager<br/>(one unit of work)"]
    B --> C["SessionFactory<br/>(metadata, caches, one per database)"]
    B --> D["Hibernate core<br/>(SQL generation, dialect)"]
    D --> E["JDBC driver + connection pool"]
    E --> F[("Relational database")]

Your code works with a Session; Hibernate turns that into JDBC calls.

The pieces you will work with all through the course:

  • SessionFactory is built once at start-up. It reads the configuration and all entity mappings, builds the metadata model, creates the second-level cache and the connection pool. It is expensive to create, thread-safe and shared by the whole application.
  • Session represents one unit of work: usually one request or one business operation. It is cheap, not thread-safe, and holds the persistence context, the set of entity objects it currently manages. Open it, use it, close it. See SessionFactory and Session.
  • Transaction groups the operations of a unit of work so they succeed or fail together. All data changes must happen inside one.
  • Dialect is the database-specific part: how to write LIMIT, which types exist, how to generate identity columns. Hibernate detects it from the JDBC connection, so you rarely set it by hand.
  • Connection provider hands out JDBC connections, from a built-in pool (testing only) or from HikariCP or an application-server DataSource.

A stand-in for the layers

Hibernate sits on JDBC, and every statement it creates ends up as a JDBC call. The runnable program below is a stand-in, not Hibernate: it wraps a Connection in a dynamic proxy that logs each prepared statement, which is conceptually how SQL logging works. The "application code" part never knows it is being observed.

Java Stand-in: watching the JDBC layer

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.

Same code, different provider

Because JPA is a contract, application code that depends only on the interface does not care what is behind it. The next stand-in makes that point with a tiny interface and two interchangeable implementations. It is an analogy: JPA is the interface, Hibernate and EclipseLink are two implementations.

Java Stand-in: one interface, two providers

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.

Which API should you use?

  • Standard JPA (EntityManager, JPQL, jakarta.persistence annotations): portable, widely documented, used by Spring Data JPA (Spring Data JPA).
  • Native Hibernate (SessionFactory, Session, HQL): gives access to everything Hibernate offers, such as stateless sessions, multi-ID loading and natural IDs. This course mostly uses Session, which is also an EntityManager, so what you learn applies to both.

A common middle path: JPA by default, unwrap(Session.class) for the few special cases. For a deeper comparison of the two APIs in an application, see Hibernate native API and JPA overview.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the relationship between JPA and Hibernate?

    Jakarta Persistence defines the standard; Hibernate implements it and adds features of its own.
  2. 2. Which object is expensive to create and shared by the whole application?

    The SessionFactory holds the metadata, caches and pool; build it once at start-up. A Session is cheap and short-lived.
  3. 3. What is the standard JPA counterpart of Hibernate's Session?

    Session extends EntityManager in current Hibernate versions, so you can unwrap one into the other.
  4. 4. Why prefer standard jakarta.persistence imports when possible?

    Using the standard API keeps the provider-specific parts small and easy to find.

Summary

  • JPA is the specification (jakarta.persistence); Hibernate is the most common implementation and adds its own features.
  • SessionFactory (built once) creates Session objects (one per unit of work); each Session is also an EntityManager.
  • Hibernate generates SQL, adapts it with a dialect and sends it through JDBC and a connection pool.
  • Code to the JPA interfaces by default and unwrap to Hibernate types only when needed.

Next: Your First Hibernate Project