
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: February 7, 2025
In this article, we’ll discuss how to bootstrap Hibernate 6 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.
Bootstrapping a SessionFactory with the native Hibernate API is a bit complicated and would take us quite a few lines of code (look at the official documentation in case you need to do that).
Fortunately, Spring supports bootstrapping the SessionFactory, so we only need a few lines of Java code or XML configuration.
Let’s get started by first adding the necessary dependencies to our pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.5.2.Final</version>
</dependency>
The spring-orm module provides the Spring integration with Hibernate:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>6.0.11</version>
</dependency>
For the sake of simplicity, we’ll use H2 as our database:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
Finally, we are going to use Tomcat JDBC Connection Pooling, which fits better for production purposes than the DriverManagerDataSource provided by Spring:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>9.0.80</version>
</dependency>
As mentioned before, Spring supports us with bootstrapping the Hibernate SessionFactory.
All we have to do is define some beans and a few parameters.
With Spring, we have two options for these configurations: Java-based and XML-based.
For using Hibernate 6 with Spring, we have to define beans for LocalSessionFactoryBean, DataSource, PlatformTransactionManager, and some Hibernate-specific properties.
Let’s create our HibernateConfig class to configure Hibernate 6 with Spring:
@Configuration
@EnableTransactionManagement
public class HibernateConf {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
{"com.baeldung.hibernate.bootstrap.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager
= new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private final Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty(
"hibernate.hbm2ddl.auto", "create-drop");
hibernateProperties.setProperty(
"hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return hibernateProperties;
}
}
As a secondary option, we can also configure Hibernate 6 with an XML-based configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="...">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="packagesToScan"
value="com.baeldung.hibernate.bootstrap.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">
create-drop
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<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>
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
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:
@Configuration
@EnableTransactionManagement
@ImportResource({"classpath:hibernate6Configuration.xml"})
public class HibernateXMLConf {
//
}
Alternatively, we can simply provide the XML file to the Spring Context if the overall configuration is purely XML.
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 {
@Autowired
private SessionFactory sessionFactory;
// ...
}
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 look at the list of supported dialects.
In this quick tutorial, we configured Spring with Hibernate 6 – with both Java and XML configuration.