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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<!-- Spring Web Mapping -->
<mvc:annotation-driven />
<context:component-scan base-package="net.roseindia.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- Spring Web Mapping End-->

<!-- Spring Hibernate Mapping -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>net.roseindia.domain.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>

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

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

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
</beans>

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;

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

@Id
@GeneratedValue
@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;

@Controller
public class EmployeeController {

@Autowired
EmployeeService service;

/* Loading Home Page */
@RequestMapping("/indexpage.htm")
public String loadIndexPage() {
return "home";
}

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

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

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

return "displayForm";
}

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

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

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

/* Delete Employee Record */
@RequestMapping("/deletedata.htm")
public String delete(Map model, HttpServletRequest request) {
System.out.println("delete..");
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.