Why is HibernateDaoSupport not recommended?
Solution 1:
Using HibernateDaoSupport
/HibernateTemplate
is not recommended since it unnecessarily
ties your code to Spring classes.
Using these classes was inevitable with older versions of Hibernate in order to integrate support of Spring-managed transactions.
However, since Hibernate 3.0.1 you don't need it any more - you can write a code against a plain Hibernate API while using Spring-managed transactions. All you need is to configure Spring transaction support, inject SessionFactory
and call getCurrentSession()
on it when you need to work with session.
Another benefit of HibernateTemplate
is exception translation. Without HibernateTemplate
the same functionality can be achieved by using @Repository
annotation, as shown in Gareth Davis's answer.
See also:
- 13.3.2 Implementing DAOs based on plain Hibernate 3 API
Solution 2:
For my money there is nothing wrong with using HibernateDaoSupport
. It isn't deprecated in spring 3.0.
Can you provide the question number that you found, it maybe they where refering to a very specific use case.
The alternative is to use the @Repository
annotation. This will wire in the same exception translation (one of the big benefits of the HibernateTemplate
) and allow you to either use your own super class or just simply to avoid extending a third party framework class.
@Repository
public class YourFooDao {
@Resource
private SessionFactory sessionFactory;
private Foo get(long id){
return (Foo) sessionFactory.getCurrentSession().get(id);
}
}