Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – JPA Buddy – JPA (partner)
announcement - icon

JPA is huge! It covers nearly every aspect of communication between relational databases and the Java application and is deeply integrated into all major frameworks.

If you're using IntelliJ, JPA Buddy is super helpful. The plugin gently guides you through the subtleties of the most popular JPA implementations, visually reminds you of JPA features, generates code that follows best practices, and integrates intelligent inspections to improve your existing persistence code.

More concretely, it provides powerful tooling to generate Spring Data JPA repositories and methods, Flyway Versioned Migrations, Liquibase Differential Changelogs, DDL and SQL statements, DTO objects, and MapStruct interfaces.

Oh, and it actually generates JPA entities from an existing database and gradually update the data model as the database evolves! Yeah.

>> Become a lot more productive with JPA Buddy

1. Overview

In this article, we'll discuss how to bootstrap Hibernate 5 with Spring, using both Java and XML configuration.

This article focuses on Spring MVC. Our article Spring Boot with Hibernate describes how to use Hibernate in Spring Boot.

2. Spring Integration

Bootstrapping a SessionFactory with the native Hibernate API is a bit complicated and would take us quite a few lines of code (have a look at the official documentation in case you really need to do that).

Fortunately, Spring supports bootstrapping the SessionFactoryso that we only need a few lines of Java code or XML configuration.

3. Maven Dependencies

Let's get started by first adding the necessary dependencies to our pom.xml:


The spring-orm module provides the Spring integration with Hibernate:


For the sake of simplicity, we’ll use H2 as our database:


Finally, we are going to use Tomcat JDBC Connection Pooling, which fits better for production purposes than the DriverManagerDataSource provided by Spring:


4. Configuration

As mentioned before, Spring supports us with bootstrapping the Hibernate SessionFactory.

All we have to do is to define some beans as well as a few parameters.

With Spring, we have two options for these configurations, a Java-based and an XML-based way.

4.1. Using Java Configuration

For using Hibernate 5 with Spring, little has changed since Hibernate 4: we have to use LocalSessionFactoryBean from the package org.springframework.orm.hibernate5 instead of org.springframework.orm.hibernate4.

Like with Hibernate 4 before, we have to define beans for LocalSessionFactoryBean, DataSource, and PlatformTransactionManager, as well as some Hibernate-specific properties.

Let's create our HibernateConfig class to configure Hibernate 5 with Spring:

public class HibernateConf {

    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          {"com.baeldung.hibernate.bootstrap.model" });

        return sessionFactory;

    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();

        return dataSource;

    public PlatformTransactionManager hibernateTransactionManager() {
        HibernateTransactionManager transactionManager
          = new HibernateTransactionManager();
        return transactionManager;

    private final Properties hibernateProperties() {
        Properties hibernateProperties = new Properties();
          "hibernate.hbm2ddl.auto", "create-drop");
          "hibernate.dialect", "org.hibernate.dialect.H2Dialect");

        return hibernateProperties;

4.2. Using XML Configuration

As a secondary option, we can also configure Hibernate 5 with an XML-based configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="...">

    <bean id="sessionFactory" 
        <property name="dataSource" 
        <property name="packagesToScan" 
        <property name="hibernateProperties">
                <prop key="hibernate.hbm2ddl.auto">
                <prop key="hibernate.dialect">

    <bean id="dataSource" 
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:mem:db;DB_CLOSE_DELAY=-1"/>
        <property name="username" value="sa"/>
        <property name="password" value="sa"/>

    <bean id="txManager" 
        <property name="sessionFactory" ref="sessionFactory"/>

As we can easily see, we're defining exactly the same beans and parameters as in the Java-based configuration earlier.

To bootstrap the XML into the Spring context, we can use a simple Java configuration file if the application is configured with Java configuration:

public class HibernateXMLConf {

Alternatively, we can simply provide the XML file to the Spring Context, if the overall configuration is purely XML.

5. Usage

At this point, Hibernate 5 is fully configured with Spring, and we can inject the raw Hibernate SessionFactory directly whenever we need to:

public abstract class BarHibernateDAO {

    private SessionFactory sessionFactory;

    // ...

6. Supported Databases

Unfortunately, the Hibernate project doesn't exactly provide an official list of supported databases.

That being said, it's easy to see if a particular database type might be supported, we can have a look at the list of supported dialects.

7. Conclusion

In this quick tutorial, we configured Spring with Hibernate 5 – with both Java and XML configuration.

As always, the full source code of the examples is available over on GitHub.

Persistence bottom

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

Persistence footer banner
Comments are closed on this article!