Saturday 17 March 2012

SPRING AND HIBERNATE INTEGRATION






   Hibernate is a famous ORM.  Spring is almost universally used light-weight container.

   I want to show how we can integrate a Hibernate bean program with Spring.

I am using Hibernate2  and Spring1.2

   First of all I created a database in MySql.
The name of the database is springhiberdb.

This database has just one table named books.
The table has three columns.
( id, author and title) ( int(3), varchar(16),varchar(16).

I made a few entries  and left it running..

  My working directory is g:\springhiber
path=d:\windows\system32;c:\jdk1.5\bin;

my classpath   includes the following files.
We must be very careful in this step. I had a lot of trouble as I missed one of the  required jar files.

g:\springhiber;


commons-collections-2.1.1.jar;
commons-logging-1.0.4.jar;
dom4j-1.4.jar;
hibernate2.jar;
jta.jar;
log4j-1.2.9.jar;
mysqldriver.jar;

cglib-full-2.0.2.jar;

spring.jar

 Any hibernate program has  a database properties file, persistence mapping file and the class to be persisted.

In my case, I am persisting book.class.

book.java

public class book
{
   Integer  id;
   String  title;
   String author;

   public   void 
    setId(Integer  id){this.id=id;}
   public   Integer
      getId(){return  id;}

   public   void
    setTitle(String  title){this.title=title;}
   public   String  getTitle(){return  title;}


   public   void
   setAuthor(String  author){this.author=author;}
   public   String  getAuthor(){return  author;}

}


 When we use Spring, we   should not provide a separate hibernate.properties file. 
 The database details of the MySql database will be mapped in beans.xml.

    Secondly, the beans.xml file will map the dialect and the class being persisted 
as HibernateProperties.
( quite reasonable to deal like this).
Thirdly, the beans.xml will map our springhibernean.

So, I give the hiberbean code now .
( Spring’s hibernatetemplate takes care of all the details!)

********************************************************************************************

import   java.util.*;
import  net.sf.hibernate.*;
import  org.springframework.dao.*;

import  org.springframework.orm.hibernate.support.*;

public  class  springhiberbean  extends  HibernateDaoSupport

{
      public  void addbook(book book1)
       {
         getHibernateTemplate().save(book1);

       }

}
**********************************************************

That is all!

So, let us now see the beans.xml    file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">


<beans>
<!--***********************************    -->
  <bean id="dataSource"
class=
"org.springframework.jdbc.datasource.
DriverManagerDataSource">
     <property name="driverClassName"
 value= "com.mysql.jdbc.Driver" />

     <property name="url" value="jdbc:mysql://localhost/springhiberdb"/>
     <property name="username" value = "root"/>
     <property name="password" value="" />
</bean>

<!--*************************************  -->
<bean id="sessionFactory"

 class="org.springframework.orm.hibernate
.LocalSessionFactoryBean">

       <property name="dataSource">
          <ref local="dataSource" />
       </property>

       <property      name="mappingResources">
           <list>
           <value>book.hbm.xml</value>
           </list>
       </property>

       <property  name="hibernateProperties">
          <props>

             <prop  key="hibernate.dialect">
              net.sf.hibernate.dialect.MySQLDialect
             </prop>

         </props>
       </property>
</bean>
<!-- ***********************************   -->


 <bean id="bean1" class="springhiberbean">
       <property name="sessionFactory">
                 <ref local="sessionFactory" />
       </property>
 </bean>

</beans>

********************************************************************************************



Next thing is mapping xml file,

book.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
        <class name="book"   table="books">

  <id name="id" column="ID"
 unsaved-value="0">
                   <generator class="increment"/>
               </id>

     <property name="title"   type="string"  />
   <property name="author"   type="string" />
        </class>

</hibernate-mapping>

That is all  there is to it.

Now create a client program as given below.













// userconsole.java

import  org.springframework.context.*;
import  org.springframework.context.support.*;
import  java.util.*;

class userconsole
{
 public static void main(String[]  args)
 {
  ApplicationContext  context  =
    new ClassPathXmlApplicationContext(  "beans.xml"  );

  springhiberbean  bean1=
       (springhiberbean)context.getBean("bean1");

  book book1  =  new book();


  book1.setTitle(args[0]);
  book1.setAuthor(args[1]);
  bean1.addbook(book1);

}

}

Now, compile all the java files and then execute the client program as
>java  userconsole   “struts”  “ted-husted”
The id will be automatically incremented.
The book is added to the table.
We can see that in mysql console.

We are not writing any code atall for hibernate!
Do you remember the typical hibernate code beginning with something like
Configurartion  config = new Configuration();
config.addClass(… etc?

We do not need all that now. Spring’s Hibernate Template takes care of such details.

====================================