Different way to configure hibernate 3 with spring?
As per my knowledge There are 3 different way to configure Hibernate 3 with spring 3. Let us sort discuss on each of three configuration method and lastly conclude witch method is best.
Method - 1
Create your concrete DAO class and extend HibernateDaoSupport which is provided by spring 3. This base class has to be configured with a SessionFactory and gives access to Hibernate Sessions and a HibernateTemplate.
Method - 2
The second method is Directly injecting a HibernateTemplate which is provided by spring 3 into your DAO class. You don't have to extend any framework class. The HibernateTemplate gives access to many general purpose DAO methods.
On the other side the HibernateTemplate is handy for automatic handling of the hibernate session management (e.g. binding the session to the current Transaction) but now in Hibernate 3 this is no longer needed as Hibernate 3 also provide this facility.
HibernateTemplate also converts any Hibernate or SQL related Exception into a corresponding DataAccessException, which is very helpful.
Third method is directly injecting the Hibernate SessionFactory into your DAO class. Your DAO won't have dependancies on any class from the Spring Framework. Since Hibernate 3 is capable of binding the Session to a preset Context (e.g. Thread or Transaction) the HibernateTemplate is no longer needed.
I think Method- 3 is the best way to configure hibernate 3 with spring, because in Method 2 and Method 3 our DAO or our application is tightly bounded with spring helper classes like HibernateTemplate and HibernateDaoSupport but in Method 3 our DAO or our application is depended on only hibernate 3 APIs.
Now question arises if we use Method 3 for configuration then how data base specific exception can be converted to DataAccessExceptions which HibernateTemplate automatically convert.The answer is very simple just annotate your DAOs with @Repository and include this Spring Bean into you ApplicationContext.
As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().
Spring Document says not use HibernateDaoSupport and HibernateTemplate. Please reffer following URL for more on this