Spring and Hibernate Declarative Transaction Management Example

By | June 24, 2021

In this post, We will learn about Spring and Hibernate Declarative Transaction Management Example using a Demo Project.

In hibernate framework, we usually provide all the database information in the hibernate.cfg.xml file.

But when we integrate the hibernate application with spring, we really don’t need to create the hibernate.cfg.xml file. We can configure all the information in the applicationContext.xml file.


We need to add Spring, Hibernate and MySQL dependencies in Maven pom.xml file


Employee entity class to store employee data


Employee Service Interface


Employee Service Implementation class


Employee DAO(Data Access Object) Interface


Employee DAO(Data Access Object) Interface implementation class to perform CRUD operations with MySQL database.

In this config file, we are providing all the information of the database in the DriverManagerDataSource object. This object is used in the LocalSessionFactoryBean class object, containing some other information such as packagesToScan and hibernateProperties. The object of LocalSessionFactoryBean class is used to inject in org.springframework.orm.hibernate5.HibernateTemplate in EmployeeDaoImpl class using setter based dependency injection

We usually manage transactions on the service layer. Here We have used Spring AOP Advice to handle the transaction. AOP Advice refers hibernateTransactionManager internally to manage the transaction. Finally, We have provided the Service layer methods for transaction handling there we have the option to provide transaction attributes like isolation & propagation levels, timeout,readOnly and rollbackFor etc..

We can also Manage transactions using annotations rather than using XML Declarative transaction management for that, we can use the below annotation either at the service class or method level with some optional attributes.



This ClientTest class gets the bean from the applicationContext.xml file and calls the different methods of  EmployeeService to perform CRUD operation with MySQL database.

If you run ClientTest.java as Java Application then it will give the below output: 

MySQL Database Screen Shot After running ClientTest:

That’s all about Spring and Hibernate Declarative Transaction Management Example

You May Also Like:

Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
Spring 5 and Hibernate 5 integration CRUD Example
Spring HibernateTempate Example

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *