当前位置: 首页 > 知识库问答 >
问题:

通过字段“Employee DAO”表示的未满足的依赖关系;

白才艺
2023-03-14
I am fresh to spring boot and currently facing this error in STS

"Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException"
Entity Class

@Entity
@Table(name = "studenttable")
public class Employee {



@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @Column(name = "sname")
    private String sname;
    @Column(name = "scourse")
    private String cname;
    @Column(name = "sfee")
    private Double fee;
Hibernate Utils Class
    @Configuration
    public class HibernateUtilsConfig {

        @Autowired
        private EntityManagerFactory entityManagerFactory;

        @Bean
        public SessionFactory getSessionFactoty() {
            if(entityManagerFactory.unwrap(SessionFactory.class)== null) {
                throw new NullPointerException("Factory Not Found");
            }
            return entityManagerFactory.unwrap(SessionFactory.class);
        }
DAO Class
@Repository
public class EmployeeDao {

    @Autowired
    private SessionFactory sessionfactory;

    public void createEmployee(Employee employee) {
        Session session = null;
        try {
            session = sessionfactory.openSession();
            session.beginTransaction();
        Integer id=(Integer)    session.save(employee);
        System.out.println("The record is add in the system" + id);
            session.getTransaction().commit();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
Main Class
@SpringBootApplication
public class SpringExampleApplication implements CommandLineRunner {

    @Autowired
    private EmployeeDao employeeDao;

    public static void main(String[] args) {
        SpringApplication.run(SpringExampleApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        Employee employee = getEmployee();
        employeeDao.createEmployee(employee);

    }
    private Employee getEmployee() {
        Employee employee = new Employee();
        employee.setSname("Imran");
        employee.setCname("Java");
        employee.setFee(1000d);
        return employee;
    }

**Error Log**

共有1个答案

晏鸿畅
2023-03-14

您面临的问题是由于您提供的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>
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
   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;
   }
 类似资料: