eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll explore the new mechanism by which we can initialize and start a Hibernate SessionFactory. We’ll especially focus on the new native bootstrapping process as it was redesigned in version 5.0.

Prior to version 5.0, applications had to use the Configuration class to bootstrap the SessionFactory. This approach is now deprecated, as the Hibernate documentation recommends using the new API based on the ServiceRegistry.

Simply put, building a SessionFactory is all about having a ServiceRegistry implementation that holds the Services needed by Hibernate during both startup and runtime.

2. Maven Dependencies

Before we start exploring the new bootstrapping process, we need to add the hibernate-core jar file to the project classpath. In a Maven based project, we just need to declare this dependency in the pom.xml file:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.24.Final</version>
</dependency>

As Hibernate is a JPA provider, this will also include the JPA API dependency transitively.

We also need the JDBC driver of the database that we’re working with. In this example, we’ll use an embedded H2 database:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.214</version>
</dependency>

Feel free to check the latest versions of hibernate-core and H2 driver on Maven Central.

3. Bootstrapping API

Bootstrapping refers to the process of building and initializing a SessionFactory.

To achieve this purpose, we need to have a ServiceRegistry that holds the Services needed by Hibernate. From this registry, we can build a Metadata object that represents the application’s domain model and its mapping to the database.

Let’s explore these major objects in greater detail.

3.1. Service

Before we dig into the ServiceRegistry concept, we first need to understand what a Service is. In Hibernate 5.0, a Service is a type of functionality represented by the interface with the same name:

org.hibernate.service.Service

By default, Hibernate provides implementations for the most common Services, and they are sufficient in most cases. Otherwise, we can build our own Services to either modify original Hibernate functionalities or add new ones.

In the next subsection, we’ll show how Hibernate makes these Services available through a lightweight container called ServiceRegistry.

3.2. ServiceRegistry

The first step in building a SessionFactory is to create a ServiceRegistry. This allows holding various Services that provide functionalities needed by Hibernate and is based on the Java SPI functionality.

Technically speaking, we can see the ServiceRegistry as a lightweight Dependency Injection tool where beans are only of type Service.

There are two types of ServiceRegistry and they are hierarchical. The first is the BootstrapServiceRegistry, which has no parent and holds these three required services:

  • ClassLoaderService: allows Hibernate to interact with the ClassLoader of the various runtime environments
  • IntegratorService: controls the discovery and management of the Integrator service allowing third-party applications to integrate with Hibernate
  • StrategySelector: resolves implementations of various strategy contracts

To build a BootstrapServiceRegistry implementation, we use the BootstrapServiceRegistryBuilder factory class, which allows customizing these three services in a type-safe manner:

BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder()
  .applyClassLoader()
  .applyIntegrator()
  .applyStrategySelector()
  .build();

The second ServiceRegistry is the StandardServiceRegistry, which builds on the previous BootstrapServiceRegistry and holds the three Services mentioned above. Additionally, it contains various other Services needed by Hibernate, listed in the StandardServiceInitiators class.

Like the previous registry, we use the StandardServiceRegistryBuilder to create an instance of the StandardServiceRegistry:

StandardServiceRegistryBuilder standardServiceRegistry =
  new StandardServiceRegistryBuilder();

Under the hood, the StandardServiceRegistryBuilder creates and uses an instance of BootstrapServiceRegistry. We can also use an overloaded constructor to pass an already created instance:

BootstrapServiceRegistry bootstrapServiceRegistry = 
  new BootstrapServiceRegistryBuilder().build();
StandardServiceRegistryBuilder standardServiceRegistryBuilder = 
  new StandardServiceRegistryBuilder(bootstrapServiceRegistry);

We use this builder to load a configuration from a resource file, such as the default hibernate.cfg.xml, and finally, we invoke the build() method to get an instance of the StandardServiceRegistry.

StandardServiceRegistry standardServiceRegistry = standardServiceRegistryBuilder
  .configure()
  .build();

3.3. Metadata

Having configured all the Services needed by instantiating a ServiceRegistry either of type BootstrapServiceRegistry or StandardServiceRegistry, we now need to provide the representation of the application’s domain model and its database mapping.

The MetadataSources class is responsible for this:

MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
metadataSources.addAnnotatedClass();
metadataSources.addResource()

Next, we get an instance of Metadata, which we’ll use in the last step:

Metadata metadata = metadataSources.buildMetadata();

3.4. SessionFactory

The last step is to create the SessionFactory from the previously created Metadata:

SessionFactory sessionFactory = metadata.buildSessionFactory();

We can now open a Session and start persisting and reading entities:

Session session = sessionFactory.openSession();
Movie movie = new Movie(100L);
session.persist(movie);
session.createQuery("FROM Movie").list();

4. Conclusion

In this article, we explored the steps needed to build a SessionFactory. Although the process seems complex, we can summarize it in three major steps: we first created an instance of StandardServiceRegistry, then we built a Metadata object, and finally, we built the SessionFactory.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)