Spring 4 + Hibernate 5 Example

Share this:
  • 1

This article is an example of java application showing the integration of  Spring 4 and hibernate 5. It explains how to configure Hibernate 5 and Spring 4 along with transaction manager to perform database operations. All configurations are done in java classes using annotations.

Our application has following features:

  • Standalone Spring application performing database operations
  • Uses Hibernate transaction manager
  • In-memory database (HSQLDB)
  • Multi-layered application with Service and DAO layer

Technology Stack

  • Spring Framework – 4.3.4.RELEASE
  • Hibernate – 5.2.5.Final
  • Database – HSQLDB
  • JDK 8


Project Dependencies

Let’s start with the project dependencies first. As per technology stack, we require following dependencies in our pom.xml


Bootstrap Application

For bootstrapping the application we have one @Configuration class which configures component scanning using @ComponentScan annotation. This class will be used to initialize spring’s application context and test the application.


Hibernate 5 Configuration

This is the main configuration of this article and will be done in a separate @Configuration class HibernateConfig.java. We are configuring following things in this class:

  • DataSource
  • SessionFactory
  • Transaction Manager

Before configuring above things, take a look at some class level configurations.

This class has two more annotations:

  1. @EnableTransactionManagement – to enable Spring’s annotation-driven transaction management capability
  2. @PropertySource – Add PropertySouce to Spring’s Environment from .properties file


And the properties file which contains all configurable properties is:


Now take a look at each configuration/Bean define in this class:


Create DataSource Bean from DriverManagerDataSource class. You need to provide driver name, database URL, username and password defined in our application.properties file.



We are using LocalSessionFactoryBean class provided by Spring to define Hibernate SessionFactory. Make sure this class is imported from org.springframework.orm.hibernate5 package. You need to set dataSource, packages to scan for model classes and various hibernate configuration properties to the instance of this class.


Transaction Manager

The last configuration is Transaction manager. We use HibernateTransactionManager provided by Spring framework. Here too, make sure this class is imported from org.springframework.orm.hibernate5 package. You need to set the SessionFactory to create earlier to this transaction manager instance.


DAO Layer

Our DAO class is annotated with @Repository. We are going add just two database operations in this class. First one to store an employee in database and second to retrieve the saved employee from the database. The SessionFactory is autowired in our DAO class. Just get the current session from SessionFactory and call save and get methods to perform respective database operations.

Service Layer

The service class is annotated with @Service and executes the respective DAO layer method from autowired DAO instance. The main thing to note in this class is @Transactional annotation which defines the transaction attribute for a class or method. At the class level, we configure it as readOnly to make all transactions in this class as read-only. This is general practice to void accidental write operation when a method should only read the data. We need to override this setting for methods that should perform write operations. For example,  addNewEmployee method in this class.

Main Application

After we have all configurations and application layers set properly, let’s start writing the main application class which makes a use of all these things. Our application is nothing but a Component class which calls service layer methods. First, it saves an employee and then retrieve the same from database in a single method performDbTasks().


Finally, we need a test class which will initialize the Spring’s application context and execute our main application.


If everything is correct you will see a log like this after executing the TestApplication.java


Source Code

The complete source code is available at https://github.com/bytestree/spring4-hibernate5-example

Share this:
  • 1