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!