Hibernate Spring Integration

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

Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1?



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.


Share on Google+Share on Google+

Hibernate Spring Integration

Posted on: July 13, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Spring Integration  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 25, 2013
output of this code

very good project