View RSS Feed

Spring Framework

Using Spring with Hibernate

Rate this Entry
by , 11-16-2011 at 06:43 PM (3307 Views)
In our previous tip, I gave an introduction to Spring’s persistence technology agnostic approach to data access. In this tip, we will show you how to use Spring with one of the most popular persistence technologies, Hibernate. Hibernate is an open source persistence framework that is one of the most successful persistence frameworks in the developer community. The reason for this is that it is a full featured object relational mapping (ORM) tool. It provides all the features you’d expect from a full-featured ORM tool, such as caching, lazy loading, eager fetching, and distributed caching. In Spring, you are provided with a HibernateTemplate that you use for data access and managing the Hibernate session. The key steps are the following:

Declare a Hibernate Session factory :

Java Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.
<property name="dataSource" ref="dataSource" /> 
<property name="packagesToScan" value=”com.acme.springexamples.domain” />
<property name="hibernateProperties">
<prop key="dialect">org.hibernate.dialect.HSQLDialect</prop>
Build your interface and implementation of the DAO:

Java Code:
package com.acme.springexamples.dao;
import org.hibernate.SessionFactory; 
import org.hibernate.classic.Session; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.acme.springexamples.Car;

public class HibernateCarDaoImpl implements CarDao{
	private SessionFactory sessionFactory;
	public HibernateCarDaoImpl(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;

	private Session currentSession() { 
		return sessionFactory.getCurrentSession();
	public void prepareCar(Car car) {
	public Car getCarById(long id) { 
		return (Car)currentSession().get(Car.class, id);
	public void saveSpitter(Car car) { 

First, note that we’re using Spring’s @Autowired annotation to have Spring automatically inject a SessionFactory into HibernateSpitterDao's sessionFactory property and the @Repository so that we don’t need to explicitly declare a HibernateCarDao bean as long as we configure <context:component-scan>:

Java Code:
<context:component-scan base-package="com.acme.springexamples.persistence" />
In addition to helping to reduce XML-based configuration, @Repository is used to catch platform- specific exceptions and rethrow them as one of Spring’s unified unchecked exceptions. So by using Hibernate contextual sessions instead of a Hibernate template, I add the PersistenceExceptionTranslationPostProcessor bean in order to add an advisor to any bean that’s annotated with @Repository so that any platform specific exceptions are caught and then rethrown as one of Spring’s unchecked data access exceptions.

Java Code:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
In our next tip, we will look at using Spring with Java Persistence API.

Submit "Using Spring with Hibernate" to Facebook Submit "Using Spring with Hibernate" to Digg Submit "Using Spring with Hibernate" to Submit "Using Spring with Hibernate" to StumbleUpon Submit "Using Spring with Hibernate" to Google