Hibernate Spring Integration

In this tutorial we will discuss how to integrate hibernate with Spring framework.

Hibernate Spring Integration

Hibernate Spring Integration

In this tutorial we will discuss how to integrate hibernate with Spring framework.

For Integration of Hibernate in spring you must have sufficient knowledge of hibernate and spring both.

Hibernate: Hibernate is ORM based persistent technology of java. It is object oriented replacement of jdbc.

Spring : Spring is an open source layered application framework.
It provides concept of Inversion-of-control and aspect Oriented Programming.

For this video tutorial you need the Eclipse IDE which is configured to use the tomcat to run the web application. If you don't have the Eclipse IDE download and install it.

Pre-requisite: You should have prior experience with web application development, Hibernate knowledge, Spring framework knowledge and the MySQL database working knowledge.

Here is the video tutorial for Integrating the Spring and Hibernate framework for making of a web application:

Hibernate Spring Integration : Take a look on directory structure of  application -

Here we are mentioning steps to create an application of Hibernate Spring integration. This example is MVC based DAO application. Employee is our table and we are including crud operations in it.

Step1. Create configuration File dispatcher-servlet.xml : In dispatcher-servlet.xml file we will configure Spring and hibernate. See the xml file given bellow to understand how to configure these.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
<!-- Enable annotation driven controllers, validation etc... -->
<!-- Spring Web Mapping -->
<mvc:annotation-driven />
<context:component-scan base-package="net.roseindia.controller" />
<bean id="viewResolver"
<property name="prefix">
<property name="suffix">
<!-- Spring Web Mapping End-->

<!-- Spring Hibernate Mapping -->
<bean id="dataSource"
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring_hibernate" />
<property name="username" value="root" />
<property name="password" value="root" />

<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>

<!-- Spring Message Resources Mapping End-->
<bean id="employeeDao" class="net.roseindia.dao.daoImpl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />

<bean id="service" class="net.roseindia.services.impl.EmployeeServiceImpl">
<property name="employeeDao" ref="employeeDao" />

<bean id="messageSource"
<property name="basename" value="/WEB-INF/messages" />

Step 2: Create persistent POJO class Employee.java ,mapped table employee using hibernate annotation .

package net.roseindia.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "employee")
public class Employee {
private int emp_id;
private String name;
private String address;
private Integer salary;

@Column(name = "emp_id")
public int getEmp_id() {
return emp_id;

public void setEmp_id(int empId) {
emp_id = empId;

@Column(name = "name")
public String getName() {
return name;

public void setName(String name) {
this.name = name;

@Column(name = "address")
public String getAddress() {
return address;

public void setAddress(String address) {
this.address = address;

@Column(name = "salary")
public Integer getSalary() {
return salary;

public void setSalary(Integer salary) {
this.salary = salary;


Step3.Create classes corresponding to each form designed in view in which you are going to perform some action. As in this application we have created two form classes Employee Form.java and SearchForm.java

Step 4: Create DAO implementation class (EmployeeDaoImpl.java) implementing our Dao interface (EmployeeDao.java)  . All the operations are defined in Dao.

Step 5. Create Service class to hold all the services provided by the application. EmployeeServiceImpl.java implements the interface EmployeeService.java.

Step 6: Main controller class controls all operation flow of application. Take a view of Controller class and download complete application from the given link.

package net.roseindia.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import net.roseindia.Form.EmployeeForm;
import net.roseindia.Form.SeachForm;
import net.roseindia.services.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

public class EmployeeController {

EmployeeService service;

/* Loading Home Page */
public String loadIndexPage() {
return "home";

/* Loading Employee Page */
public String loadEmployeeForm(Map model, EmployeeForm employeeForm) {
model.put("employeeForm", employeeForm);
return "employeePage";

/* Add new Employee */
public String saveEmployee(Map model, @Valid EmployeeForm employeeForm,
BindingResult result) {
if (result.hasErrors()) {
return "employeePage";
} else {
if (employeeForm.getId() >= 0) {
} else {
return "redirect:/display.htm";

/* Display employee record */
public String displayForm(Map model, EmployeeForm employeeForm) {
model.put("employee", service.displayEmployee(employeeForm));

return "displayForm";

/* Load Search Form */
public String loadSearchForm(Map model, SeachForm seachForm) {
model.put("seach", seachForm);
return "searchForm";

/* Display searched employee details */
public String findEmployee(Map model, SeachForm seachForm) {
model.put("employee", service.findEmployee(seachForm));
return "displayForm";

/* Edit record of employee */
public String searchForEdit(Map model, EmployeeForm employeeForm,
HttpServletRequest request) {
model.put("employeeForm", service.findEmployee(new Integer(request
return "employeePage";

/* Delete Employee Record */
public String delete(Map model, HttpServletRequest request) {
service.deleteEmployee(new Integer(request.getParameter("id")));
return "redirect:/display.htm";

Click here to download complete source code

Download the source code of the application in the Eclipse Project format so that you can run it easily from the Eclipse IDE.