Wednesday, October 16, 2013

Using HIBERNATE with SPRING (HibernateTemplate)

HibernateTemplate:

      Spring offers HibernateTemplate to simplify implementation of the Hibernate data access code.
HibernateTemplate includes various methods that reflect the Hibernate Session methods.

- It takes the responsibility to manage the Hibernate Session making the application free from implementing the code for opening and closing the Hibernate session accordingly.
- It provides a service - to convert the HibernateException's into DataAccessExceptions.

Spring framework includes two HibernateTemplate classes
1) org.springframework.orm.hibernate.HibernateTemplate  -  to work with hibernate2.1
2) org.springframework.orm.hibernate3.HibernateTemplate  - to work with hibernate3

Note: spring framework from its 2.5 version onwards supports to work with Hibernate3.1 or higher version only.

To use HibernateTemplate object, you must pass SessionFactory object to it and then use.

There are 3 constructors in HibernateTemplate class
1) HibernateTemplate()
     - you must pass SessionFactory object before you use any method of this object using setSeesionFactory() method.

2) HibernateTemplate(SessionFactory)
3) HinernateTemplate(SessionFactory, boolean)
  - if you pass true as boolean value... HibernateTemplate perform its own Session management instead of participating in a custom Hibernate current session context.



It is not recommended to use now these days. ***

source:
Spring and Hibernate (SANTOSH KUMAR K)

No comments:

Post a Comment