如何在Hibernate 5中自动连线SessionFactory?
我尝试autowire SessionFactory,但出现以下错误:如何在Hibernate 5中自动连线SessionFactory?,hibernate,spring-boot,kotlin,hibernate-5.x,Hibernate,Spring Boot,Kotlin,Hibernate 5.x,我尝试autowire SessionFactory,但出现以下错误: APPLICATION FAILED TO START *************************** Description: Field bookRepository in com.test.app.BookService required a bean named 'entityManagerFactory' that could not be found. 我在这里介绍autowire SessionF
APPLICATION FAILED TO START
***************************
Description:
Field bookRepository in com.test.app.BookService required a bean named 'entityManagerFactory' that could not be found.
我在这里介绍autowire SessionFactory:
@Service
class TestClass{
@Autowired
lateinit var sessionFactory: SessionFactory
......
}
这是我的配置类:
@Configuration
class SpringConfig {
@Bean
fun sessionFactory(emf: EntityManagerFactory): HibernateJpaSessionFactoryBean {
val fact = HibernateJpaSessionFactoryBean()
fact.entityManagerFactory = emf
return fact
}
}
我试着这样做:
@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
休眠版本:5.4.1
Spring boot版本:2.0.0.M3为什么需要一个
SessionFactory
?只需插入一个EntityManager
即可使用JPA而不是普通的Hibernate。这基本上也是您的异常情况告诉您的,它抱怨的是EntityManagerFactory
而不是SessionFactory
@M.Deinum我处理大量数据,需要提高性能。我需要会话方法saveorupdateth,它不会提高您的性能。正确的批处理技术将使我尝试批处理,但仍然不够。所以我想尝试保存或更新你尝试了什么?如前所述,saveOrUpdate不会帮助您信任我。正确的批处理技术(清除和刷新实体管理器)将起作用。为什么需要会话工厂
?只需插入一个EntityManager
即可使用JPA而不是普通的Hibernate。这基本上也是您的异常情况告诉您的,它抱怨的是EntityManagerFactory
而不是SessionFactory
@M.Deinum我处理大量数据,需要提高性能。我需要会话方法saveorupdateth,它不会提高您的性能。正确的批处理技术将使我尝试批处理,但仍然不够。所以我想尝试保存或更新你尝试了什么?如前所述,saveOrUpdate不会帮助您信任我。适当的批处理技术(清除和刷新实体管理器)将起作用。