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.

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

Add the following line in application.properties


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