0
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**

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name springExampleApplication: Unsatisfied dependency expressed through field 'employeeDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error` creating bean with name 'employeeDao': 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? at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at com.imran.works.SpringExampleApplication.main(SpringExampleApplication.java:18) [classes/:na] Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': 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? at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]

Please help me,

Thank you!

Jaymin
  • 2,879
  • 3
  • 19
  • 35
imran fida
  • 15
  • 1
  • 6

1 Answers1

0

The issue that you are facing is due to the HibernateUtilsConfig.java configuration class that you have provided.In your EmployeeDao class you are autowiring the sessionfactory bean. So , when springboot tries to autowire the bean , it fails with the following error :

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?

becase entityManagerFactory bean is not available.

Since you are using spring-boot , you may not configure everything manually. You can use the default auto configurations from spring-boot by adding the following dependency :

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

then , you could provide the appropriate keys in the application.properties or application.yml and spring-boot will configure everything for you.

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

If you still want to set everything up manually try creating the entity manager bean like :

   @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;
   }

Reference documentation.

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39