I'm using spring boot and i want to integrate spring with hibernate. I want to make a Session Factory bean for further using. But I can't autowire EntityManagerFactory, I can't autowire it only in the configuration class, in other classes it works. Can you help, please?
Configuration class
package kz.training.springrest.configuration;
@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public SessionFactory getSessionFactory() {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}
dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency
</dependencies>
EntityManagerFactoryis specially injected with@PersistenceUnit. Second, why are you wanting to do this manually when the standard Hibernate interface is now JPA? Last, even if you got the code shown working, you'd essentially be wiping out Spring's thread-trackedEntityManagerhandling. - chrylis -cautiouslyoptimistic-