Integrate Hibernate and Spring frameworks
Integrating Hibernate with Spring allows you to leverage Spring’s powerful dependency injection and transaction management features while using Hibernate for ORM (Object-Relational Mapping). Spring provides several classes and utilities to simplify the integration, such as LocalSessionFactoryBean, HibernateTransactionManager, and support for declarative transactions.
Steps to Integrate Hibernate with Spring:
- Â Add Dependencies : Include the necessary Spring and Hibernate dependencies in your project.
- Configure DataSource : Define a DataSource bean to manage database connections.
- Â Configure SessionFactory : Define a LocalSessionFactoryBean to create Hibernate SessionFactory.
- Â Configure Transaction Management : Use HibernateTransactionManager for managing transactions.
- Â Create DAOs and Service Classes : Define DAO (Data Access Object) and service classes to interact with the database.

Table of Contents
Java Example
Let’s go through a step-by-step example to integrate Hibernate with Spring.
Step 1: Add Dependencies
Maven pom.xml
xml
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.10</version>
</dependency>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.10</version>
</dependency>
<!-- Hibernate Core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.32.Final</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
Step 2: Configure DataSource
applicationContext.xml
xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DataSource Configuration -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<!-- SessionFactory Configuration -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.example.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Transaction Manager Configuration -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Enabling annotation-driven transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Step 3: Create Entity
MyEntity.java
java
package com.example.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and setters
}
Step 4: Create DAO
MyEntityDAO.java
java
package com.example.dao;
import com.example.model.MyEntity;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class MyEntityDAO {
@Autowired
private SessionFactory sessionFactory;
public void save(MyEntity entity) {
sessionFactory.getCurrentSession().save(entity);
}
}
Step 5: Create Service
MyEntityService.java
java
package com.example.service;
import com.example.dao.MyEntityDAO;
import com.example.model.MyEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class MyEntityService {
@Autowired
private MyEntityDAO myEntityDAO;
@Transactional
public void save(MyEntity entity) {
myEntityDAO.save(entity);
}
}
Step 6: Main Application
Step 6: Main Application
Main.java
java
import com.example.model.MyEntity;
import com.example.service.MyEntityService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyEntityService service = context.getBean(MyEntityService.class);
MyEntity entity = new MyEntity();
entity.setName("Test Entity");
service.save(entity);
}
}
Explanation of the Hibernate and Spring frameworks
- Add Dependencies : Add Spring and Hibernate dependencies to the pom.xml file.
- Configure DataSource : Define a DataSource bean in applicationContext.xml for database connection.
- Configure SessionFactory : Define a LocalSessionFactoryBean to create a Hibernate SessionFactory.
- Configure Transaction Management : Use HibernateTransactionManager for managing transactions.
- Create Entity : Define an entity class MyEntity.
- Create DAO : Create a DAO class MyEntityDAO to handle database operations.
- Create Service : Define a service class MyEntityService to manage business logic and transactions.
- Main Application : Use Spring’s ApplicationContext to get the service bean and perform operations.
This example demonstrates how to integrate Hibernate with Spring, leveraging Spring’s dependency injection and transaction management capabilities to simplify data access and management.