SpringBoot : How to use & configure SessionFactory Bean

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.
  1. @Bean  
  2. public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){  
  3.     return hemf.getSessionFactory();  
  4. }  

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.
  1. class Sample {  
  2.    
  3.    @Autowired  
  4.    SessionFactory sessionFactory;  
  5.   
  6.    // use the session factory as   
  7.    // sessionFactory.getCurrentSession(); to get the current session  
  8.    //  
  9. }  

No comments :

Post a Comment