1 回答
TA贡献1864条经验 获得超2个赞
您面临的问题是由于HibernateUtilsConfig.java您提供的配置类引起的。在您的 EmployeeDao 类中,您正在自动装配sessionfactorybean。因此,当 springboot 尝试自动装配 bean 时,它会失败并出现以下错误:
Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateUtilsConfig': Unsatisfied dependency expressed through field 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'getSessionFactoty': Requested bean is currently in creation: Is there an unresolvable circular reference?
因为entityManagerFactorybean 不可用。
由于您使用的是 spring-boot ,因此您可能无法手动配置所有内容。您可以通过添加以下依赖项来使用 spring-boot 的默认自动配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
然后,您可以在 application.properties 或 application.yml 中提供适当的键,spring-boot 将为您配置所有内容。
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=mysqluser
spring.datasource.password=mysqlpass
spring.datasource.url=jdbc:mysql://localhost:3306myDb?createDatabaseIfNotExist=true
如果您仍想手动设置所有内容,请尝试创建实体管理器 bean,例如:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.example.persistence.model" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
添加回答
举报