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;
}
添加回答
舉報