Points To Remember

  • Add the SessionFactory bean in the Application class.
  • Add the Current Session Context class in application.properties.
  • Use the SessionFactory using @Autowired annotation.

How to Configure & Use SessionFactory Bean

Add the following to the Main Application class or Configuration class.

@Bean  
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){ 
    return hemf.getSessionFactory();  
}

Add the following line in application.properties

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

Now you can use the Session Factory in your code as follows.

class Sample {
      @Autowired SessionFactory sessionFactory; 
      // use sessionFactory.getCurrentSession(); to get the current session 
}