Tuesday, 3 May 2011

Hibernate Session Management in Spring

SpringSource came up with a set of template-based patterns (JdbcTemplate, HibernateTemplate, JpaTemplate, RestTemplate, etc) which basically takes care of all the necessary housekeeping, allowing you to concentrate on the logic of your application...anyways, I'm going to take a practical approach to hibernate session management in Spring using SpringHibernateTemplate.

Before we start, it's important to note that as of Spring 3 documentation, it is no longer recommended to use HibernateTemplate as it unnecessarily ties your code to Spring classes.

SpringHibernateTemplate is a Spring wrapper around the native Hibernate API. It adds an extra layer of abstraction on top of Hibernate making life a bit easier specially when it comes to session management.

3 Basic Steps:

1. Hibernate Mapping File:
Create the standard hibernate mapping file(s) for your class bean(s) just as you would on any Hibernate project, nothing interesting here, business as usual.

  • DB.CUSTOMER >> Customer.java >> Customer.hbm.xml

2. DAO Implementation:
Here is where it all starts, define a second class CustomerSpringDao that extends HibernateDaoSupport so we can include database operations.

public class CustomerSpringDao extends HibernateDaoSupport
 public void addCustomer(Customer customer) {

 public Customer getCustomerAccountInfo(Customer customer) {
  Customer cust = null;
  List list = getHibernateTemplate().find("from Customer customer where customer.userId = ?" , customer.getUserId(),Hibernate.STRING);

  if(list.size() > 0){
   cust = (Customer) list.get(0);

  return cust;

This is a convenient super class for Hibernate-based data access objects. By extending this Spring abstract class you can make use of methods such as getHibernateTemplate() where you can perform hibernate operations without having to manage sessions manually. The SessionFactory is pre-initialized by the framework. Yep, that's right, it is completely abstracting the sessionFactory.openSession(), session.flush(), etc.

3. JDBC DataSource and HibernateSessionFactory Wiring
In the Spring framework, resources such as JDBC DataSource and HibernateSessionFactory can be realised as beans in the application context. Create the Spring Application Context file (if you haven't already created one) and place it in the classpath of your project. This file should reference your CustomerSpringDao class.

Putting it all toghether

Once you've completed all 3 steps, you will be ready to put it all into action. The following is an example on how you can access those spring beans and make use of spring's session management.

Before anything else, you can load the Spring application context XML file into a BeanFactory using the ClassPathXmlApplicationContext class, then go ahead persisting your object using common hibernate methods:

ClassPathXmlApplicationContext context = new

CustomerSpringDao csd = (CustomerSpringDao) context.getBean("eventDao", CustomerSpringDao.class);

// create customer object


Note, you're still free to use hibernate session if you like, all you have to do is access the hibernate session factory bean:

context.getBean("mySessionFactoryBean", SessionFactory.class);