Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HibernateTemplate vs HibernateDaoSupport vs SessionFactory Injection

I have seen in lot of forums and still in confusion. We are starting a new project with Spring 3.1 & Hibernate 4 and need to decide which strategy to use for Hibernate with Spring:

  1. Accessing Hibernate directly

    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction();

  2. Using HibernateTemplate

    List bb = (List)hibernateTemplate.find("from Entity");

  3. Using HibernateDAOSupport classes

    List bb =(List)getHibernateTemplate().find("from Entity");

Can you please help what should i use? I have read from CodeRanch and one another link which tell that from Hibernate 3.x onwards we should inject SessionFactory in our DAO Classes(using @Repository).

Can someone explain this in detail?

Regards,

Arun Kumar

like image 345
Arun Kumar Avatar asked Dec 08 '22 20:12

Arun Kumar


1 Answers

Spring itself recommends not using HibernateTemplate anymore, in the javadoc of the class. You can declare the session factory as a Spring bean directly, inject it as any other Spring bean in your own components, and use the native Hibernate API directly (using sessionFactory.getCurrentSession()).

like image 89
JB Nizet Avatar answered Jun 06 '23 15:06

JB Nizet