I'm trying to create the Hibernate's SessionFactory bean from JPA's EntityManager with the following lines But am getting EntityManger is null.I don't want to extend the JPARepository.Hences creating bean for SessionFactory in configuration class.
@Configuration
public class BeanConfig {
@Autowired
EntityManager entityManager;
@Bean
public SessionFactory getSessionFactory() {
if (entityManager == null) {
logger.info("EntityManager is null---");
} else {
if (entityManager.unwrap(Session.class) == null) {
return entityManager.unwrap(Session.class).getSessionFactory();
}
}
return null;
}
}
Logger Info printed in console: EntityManager is null---
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies><br>
application.properties
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect