1

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

Olesia Ilchuk
  • 314
  • 2
  • 5
  • 16
  • Why do you want a `SessionFactory`? Just inject an `EntityManager` to use JPA instead of plain Hibernate. This is basically also what your exception is telling you, it is complaining about an `EntityManagerFactory` NOT a `SessionFactory`. – M. Deinum Sep 12 '17 at 13:08
  • @M.Deinum I work with big amount of data and need increase performance. I need Session method saveOrUpdate – Olesia Ilchuk Sep 12 '17 at 13:31
  • That won't increase your performance. Proper batching techniques will. – M. Deinum Sep 12 '17 at 13:33
  • @M.Deinum I try batch, but it is still not enough. So I wanna to try saveOrUpdate – Olesia Ilchuk Sep 14 '17 at 08:30
  • 1
    What did you try? As mentioned saveOrUpdate won't help you trust me. Proper batching techniques (clearing and flushing the entity manager) will. – M. Deinum Sep 14 '17 at 08:33
  • 1
    @M. Deinum It would be helpful if you post the answer to the relevant question, if you know. Arguing why someone needs it is irrelavant and upto them. – Kannan Ramamoorthy Jun 27 '22 at 09:11

0 Answers0