1) Introduction
Hibernate is a powerful technology for persisting data in any kind of Application. Spring, on the other hand is a dependency injection framework that supports IOC. The beauty of Spring is that it can integrates well with most of the prevailing popular technologies. In this article, we will discuss on how it is possible to integrate Spring with Hibernate. This article assumes that the reader has a basic understanding in both Spring and Hibernate Frameworks.
If you are new to Spring and Hibernate frameworks, please read the introduction articles on Spring and Hibernate before start reading this article. Raja has explained in Introduction to Spring Framework. This article will help you to understand the fundamentals of the Spring framework. In another article Introduction to Hibernate published on 12/05/2007 by Raja explains what is ORM framework and how to start writing the simple hibernate application. (Recommended for: Spring Books and Hibernate Books)
2) Spring and Hibernate
As a pre-requisite, let us understand the need for such integration before we actually get into the integration between these two technologies. It is well known that Hibernate is a powerful ORM tool that lies between Application and Database. It enables Application to access data from any database in a platform-independent manner. There is no need for the Application to depend on the low-level JDBC details like managing connection, dealing with statements and result sets. All the necessary details for accessing a particular data source is easily configurable in Xml files. Another good thing is that Hibernate can be coupled well with both J2SE and J2EE Applications.
One of the problem with using Hibernate is that the client Application that accesses the database using Hibernate Framework has to depend on the Hibernate APIs like Configuration, SessionFactory and Session. These objects will continue to get scattered across the code throughout the Application. Moreover, the Application code has to manually maintain and manage these objects. In the case of Spring, the business objects can be highly configurable with the help of IOC Container. In simple words, the state of an object can be externalized from the Application code. It means that now it is possible to use the Hibernate objects as Spring Beans and they can enjoy all the facilities that Spring provides.
3) Integration Sample
Instead of looking into the various Integration APIs that are available in the Spring Bundle, let us study and understand these APIs as we go through the sample code. The following sections cover the various steps involved in the Spring-Hiberante integration along with a detailed explanation.
3.1) Creating Database
The following sample application uses the MySql database for dealing with data. MySql database can be downloaded from http://dev.mysql.com/downloads/mysql/5.0.html#downloads. After installing the database, start the MySql client and create a test database by issuing the following command,
Create database samples;
Note that the character ';' is the statement terminator for every command. Once the 'samples' database is created, use the database for creating tables by using the command,
Use samples;
This uses the 'samples' database for the current database session. It means that whatever operation we do, such as creating tables, will eventually affect the 'samples' database. Now, let us create a sample table called 'employee' which is having four fields namely id, name, age and salary. The following command creates the 'employee' table in the 'samples' database,
create table employee(id varchar(10), name varchar(20), age int(3), salary int(10));
Now an empty table (table with no records within it) is created.
3.2) The Employee class
Now let us create a class called Employee for storing the data that are fetched from the employee table. The class design is such that the column names for the table 'employee' will be mapped as the variable names in the Java class with the appropriate data type. The complete code listing for the Employee class is as follows,
Employee.java
package javabeat.spring.hibernate;
public class Employee {
private String id;
private String name;
private int age;
private double salary;
public Employee() {
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
public String toString(){
return "Id = " + id + ", Name = " + name + ", Age = "
+ age + ", Salary = " + salary;
}
}
Note that the toString() method is overridden to give a meaningful display for the employee object.
Spring and Hibernate Framework Articles & Books
|