Tuesday, October 18, 2011

Hibernate with Spring

Spring provides support for Hibernate mainly in two flavors. They are HibernateTemplate and HibernateDaoSupport. Let us look into both of them one by one. Let us go to the package com.demo.hibernateWithSpring and look into the files in that package.

HibernateTemplate:

HibernateTemplate provides an abstract layer over a Hibernate Session. It's main objective is to simplify the work of opening and closing hibernate sessions.

XML Spring configuration:

Look at the xml file SpringConfigurationForHibernate.xml

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name="mappingResources">
<list>
<value>com/demo/hibernateWithSpring/hibernateWithSpring.hbm.xml
</value>
</list>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>

<bean id="carDao" class="com.demo.hibernateWithSpring.CarHBTemplateDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>

* If we are mapping hibernate using XML mapping files then Spring's
LocalSessionFactoryBean provides a bean that will refer the mapping metadata from one or more of the xml mapping files.

* Hence
mySessionFactory takes a reference to the org.hibernate.SessionFactory.

* The dataSource property refers to any implementation of javax.sql.DataSource

* The
configLocation locates the location of the hibernate.cfg.xml. * mappingResources defines the list of one or more mapping files in the classpath. * The hibernateTemplate defines the instance of the class HibernateTemplate which is an abstract layer for the Hibernate Session.

* When the instance of the class CarHBTemplateDao is created from Spring xml, it will inject the reference to the HibernateTemplate into the CarHBTemplateDao object. Hence the hibernateTemplate defined in the CarHBTemplateDao class will actually provide methods to deal with the Hibernate session. (i.e) the session factory bean is wired into the HibernateTemplate and the HibernateTemplate is wired into the CarHBTemplateDao.

* In the example we have used the methods save(), load() and find() from the hibernateTemplate.

The DAO class:

public class CarHBTemplateDao {

private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
// remaining code ...
}

HibernateDaoSupport:

Spring offers a convenient DAO support class that enables us to directly wire a session factory bena into the DAO class. Actuallt HibernateDaoSupport creates a HibernaterTemplate for us to use. All we have to do is extending this HibernateDaoSupport class in our DAO classes as given below.


public class CarHBDaoSupportDao extends HibernateDaoSupport {

// remaining code ...
}

We don't have to deal with the hibernateTemplate any more. We can use the getHibernateTemplate() to access the same methods we accessed using hibernateTemplate. XML Spring Configuration: quot;com.demo.hibernateWithSpring.CarHBDaoSupportDao"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> * when the instance of the CarHBDaoSupportDao class is created using Spring, we should inject the reference of sessionFactory into the DAO class as given above.

Execute the classes SpringWithHibernateTemplateExecutor.java and SpringWithHibernateDaoSupportExecutor.java from the package and verify the results by running the simple select query on the table CARS as follows.


Spring's support for Hibernate with Annotation:

Let us go to the package annotation.com.demo.hibernateWithSpring and execute the classes Spring_AnnotationHbTemplateExecutor.java and Spring_AnnotationHbDaoSupportExecutor.java

Things to look into:

* The annotated ORM class Friend.java
* The spring configuration file SpringForHibernateAnnotation.xml

@Entity
@Table(name="FRIENDS")
public class Friend {

@Id
@GeneratedValue
@Column(name="ID")
private long id;

@Column(name="FRIEND_NAME")
private String name;

@Column(name="AGE")
private int age;

//getter and setter methods
}

The Spring configuraton xml:

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list> <value>annotation.com.demo.hibernateWithSpring.Friend</value>
</list>
</property>
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
</bean>

* If we are using the Hibernate with annotation, then spring provides sessionFactory instance using the AnnotationSessionFactoryBean.
* The annotatedClasses tag defines the list of annotated classes that are used for mapping hibernate.
* All the rest of the spring configurations remain the same as we used hibernate with xml mapping.

Execute the Spring_AnnotationHbTemplateExecutor.java and Spring_AnnotationHbDaoSupportExecutor.java classes and verify the results in the eclipse console and as well as in the HSQL DB manger by running a simple select query on the table FRIENDS as follows.