To autowire Hibernate SessionFactory in the Spring Boot application, the SpringSessionContext - implementation of the Hibernate’s CurrentSessionContext interface - can be used.
1. Add entry in application.properties file
Specify spring.jpa.properties.hibernate.current_session_context_class property with SpringSessionContext class as the value.
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
2. Bean definition
Create a bean that will attach HibernateJpaSessionFactoryBean to the container.
@Configuration
public class DBConfig {
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
}
3. Autowire the SessionFactory on the data layer
@Repository
public class OrderDaoImpl implements OrderDao {
@Autowired
private SessionFactory sessionFactory;
// rest of code removed for brevity
}