Wednesday, May 20, 2009

Hibernate integration with Spring

In my last post Basic Hibernate Annotations example, I wrote about a simple hibernate example. Today I will take that example forward and show you how you can integrate hibernate with Spring. I used Spring version 2.5.6.SEC01.

Create spring-context.xml file. We define a spring datasource, hibernate session factory, hibernate template(spring wrapper api of hibernate session), transaction manager. Bean airlineDao is configured to use annotation based transaction handling. The "<tx:annotation-driven/>" actually enables Spring container to execute methods under transaction, if transaction annotations are used.

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/travel"/>
<property name="resourceRef" value="true" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property>
<property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property>
</bean>

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

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>

<tx:annotation-driven/>

<bean id="airlineDao" class="com.travel.dao.AirlineDao">
<property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property>
</bean>
</beans>

Create AirlineDao class. Notice the @Transactional (readOnly = true) annotation which brings the execution of this class under transaction. The create method have (readOnly = false) because in this method we are actually updating the table.

package com.travel.dao;

import com.travel.domain.Airline;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Transactional (readOnly = true)
public class AirlineDao extends HibernateDaoSupport {
@Override
@Transactional (readOnly = false)
public void create(String code, String name) {
Airline airline = new Airline();
airline.setCode(code);
airline.setName(name);
getHibernateTemplate().save(airline);
}
}

Add the below xml in your web.xml. This Spring ContextLoaderListener initializes the spring beans defined in file spring-context.xml (we created above).

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-context.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Create a simple servlet to test the DAO. The execution of this servlet will create a new airline record in airline table.

package com.travel.servlet;

import com.kaleconsultants.travel.dao.AirlineDao;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AirlineServlet extends HttpServlet {
private static final long serialVersionUID = 2409603422056073641L;

protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

String code = request.getParameter("code");
String name = request.getParameter("name");
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
AirlineDao airlineDao = (AirlineDao) context.getBean("airlineDao");
airlineDao.create(code, name);
}
}

No comments:

Post a Comment