Using Hibernate SessionFactory in Spring Boot App

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 file

Specify property with SpringSessionContext class as the value.

2. Bean definition

Create a bean that will attach HibernateJpaSessionFactoryBean to the container.

public class DBConfig {

	public HibernateJpaSessionFactoryBean sessionFactory() {
	    return new HibernateJpaSessionFactoryBean();

3. Autowire the SessionFactory on the data layer

public class OrderDaoImpl implements OrderDao {
	private SessionFactory sessionFactory;
	// rest of code removed for brevity