Hibernate Spring Integration

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

Ads

Tutorials   
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 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment
Ads

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.

Advertisements

Ads
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

Advertisements

Ads
 

Discuss: Hibernate Spring Integration  

Post your Comment


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

very good project

Ads

 

Ads