How autowire SessionFactory in Hibernate 5?

729 views Asked by At

I try autowire SessionFactory, but get this error:

APPLICATION FAILED TO START
***************************

Description:

Field bookRepository in com.test.app.BookService required a bean named 'entityManagerFactory' that could not be found.

Here I autowire SessionFactory:

@Service
class TestClass{
    @Autowired
    lateinit var sessionFactory: SessionFactory
......
}

This is my config class:

@Configuration
class SpringConfig {
    @Bean
    fun sessionFactory(emf: EntityManagerFactory): HibernateJpaSessionFactoryBean {
        val fact = HibernateJpaSessionFactoryBean()
        fact.entityManagerFactory = emf
        return fact
    }
}

I try this way to:

@Configuration
class SpringConfig {
    @Bean
    fun sessionFactory(): HibernateJpaSessionFactoryBean {
        return HibernateJpaSessionFactoryBean()
    }
}

My application.yml:

spring:
    datasource:
        driver-class-name: org.postgresql.Driver
        url: datasource_url
        password: password
        username: username
        hikari:
            maximum-pool-size: 5
    jpa:
        properties:
            hibernate:
                jdbc:
                    batch_size: 20
                current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext

Hibernate version: 5.4.1

Spring boot version: 2.0.0.M3

0

There are 0 answers