Monday 15 March 2010

Advantages of Spring and Hibernate Integration

"Spring Integrates very well with Hibernate". So what? One might think. Spring integrates very well with lots of technologies, what benefits we get out of Hibernate integration. I'm trying to list out important benefits that applications get from Spring and hibernate integration.

1. Services layer

The most important benefit is the Spring framework itself. Because of the Spring integration, applications can leverage all the features of spring framework. For example POJO style serve interfaces, IoC, Aspects, Remoting etc...

Spring framework provides all these features out-of-box. And a good thing about Spring framework is that it's really easy to plug any custom component into the framework.

With spring POJOs, IoC and Remoting it's very easy to develop services layer. Otherwise, we need to implement EJB Session Bean (stateful or stateless) or depend on the frameworks like JSF, Struts or Servlets to implement the business logic which is not ideal. All of these frameworks business logic from Presentation layer or tedious to develop and maintain (Struts, for example).

2. HibernateTemplate

Spring provides template for managing sessions, transaction across the application. Without the hibernate template, applications need to manage these on their own.

Note:
With the Hibernate 3.0.1, Spring can manage the transactions and session management across the application without the template. Only thing you don't get without Hibernate template is Exception translation. But there are other options.

3. HibernateDaoSupport

Spring provides a class called HibernateDaoSupport. DAO Implementation class implements this class to get all the convenience methods that HibernateTemplate provides. If applications are not using HibernateTemplate, then it's good to take advantage of HibernateDaoSupport.

Note: Important thing to note is that any DAO wants to use HibernateDaoSupport needs to extend the class. So, it's always better to have a generic DAO that is super class for all the DAO's in the model and super class extends HibernateDaoSupport.

4. DAOException Translation

Hibernate 3 throws Runtime exceptions unlike checked exceptions thrown in the previous releases. Spring can translate these exceptions into SpringDAO exceptions and map them into Spring DAO exception hierarchy. But you have to use one of the following to get this

1. Use HibernateTemplate
2. @Repository

We discussed about the template earlier. Now let us look at the @Repository

@Repository annotation is auto-detected by the Spring’s exception translator post processor and does the transforms Hibernate exceptions into Spring DAO exceptions.

In this way you can go template-less in your spring hibernate development.

5. MVC Integration

If you are using Hibernate in you web application, which is using Spring MVC, then you can use the OpenSessionInViewFilter or OpenSessionInViewInterceptor and not create sessions per thread, instead create a session per Request.

Above are the important advantages that applications out of Spring integration with hibernate.