title: UnsatisfiedDependencyException: error creating bean
body: <p>I'm creating an ecommerce website using springMVC and hibernate. How ever I'm getting the following errors while running the junit test case. I'd really appreciate any help provided for the same.</p>
  
  <p>-----------Exception Trace-----------</p>
  
  <pre><code>        org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDao': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.yusufjk.GrocersBasketBackend.config.HibernateConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.yusufjk.GrocersBasketBackend.domain.Product.category in com.yusufjk.GrocersBasketBackend.domain.Category.product
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
      at com.yusufjk.GrocersBasketBackend.AccountDaoTest.initialize(AccountDaoTest.java:23)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
      at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.yusufjk.GrocersBasketBackend.config.HibernateConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.yusufjk.GrocersBasketBackend.domain.Product.category in com.yusufjk.GrocersBasketBackend.domain.Category.product
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
      ... 26 more
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.yusufjk.GrocersBasketBackend.domain.Product.category in com.yusufjk.GrocersBasketBackend.domain.Category.product
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
      ... 38 more
  Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.yusufjk.GrocersBasketBackend.domain.Product.category in com.yusufjk.GrocersBasketBackend.domain.Category.product
      at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:775)
      at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725)
      at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
      at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
      at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
      at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
      at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
      at com.yusufjk.GrocersBasketBackend.config.HibernateConfig.sessionFactory(HibernateConfig.java:32)
  </code></pre>
  
  <p>Here is the junit test file</p>
  
  <p>AccountDaoTest.java:</p>
  
  <pre><code> package com.yusufjk.GrocersBasketBackend;
  
  import static org.junit.Assert.*;
  
  import org.junit.BeforeClass;
  import org.junit.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  import com.yusufjk.GrocersBasketBackend.dao.AccountDao;
  import com.yusufjk.GrocersBasketBackend.domain.Account;
  
  public class AccountDaoTest {
  
      @Autowired 
      static AnnotationConfigApplicationContext context;
  static AccountDao acDao;
  @BeforeClass
  public static void initialize() {
  
      context=new AnnotationConfigApplicationContext();
      context.scan("com.yusufjk");
      context.refresh();
  
      acDao= (AccountDao)context.getBean("userDao");
  }
  @Test
  public void accountTestCase() {
  
      Account account=new Account();
      account.setUid(1);
      account.setUname("yusufjk");
      account.setRole("admin");
      account.setUemail("nnoreply54@gmail.com");
      account.setUpassword("12345");  
  assertEquals(account.getUid(), account.getUid());
  }
  }
  </code></pre>
  
  <p>HibernateConfig.java:</p>
  
  <pre><code>    package com.yusufjk.GrocersBasketBackend.config;
  import java.util.Properties;
  import javax.sql.DataSource;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate5.HibernateTransactionManager;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  
  
      @Configuration
      @EnableTransactionManagement
      @ComponentScan( "com.yusufjk.GrocersBasketBackend" )
  
      public class HibernateConfig {
  
          @Autowired 
          @Bean(name="sessionFactory")
          public SessionFactory sessionFactory(DataSource dataSource) {
              LocalSessionFactoryBuilder sessionFactory = new LocalSessionFactoryBuilder(dataSource);
              sessionFactory.addProperties(hibernateProperties());
              sessionFactory.scanPackages("com.yusufjk");
              return sessionFactory.buildSessionFactory();
           }
  
  
          @Autowired
          @Bean(name="datasource")
          public DataSource dataSource() {
              System.out.println("inside dataSource");
              DriverManagerDataSource dataSource = new DriverManagerDataSource();
              dataSource.setDriverClassName("org.h2.Driver");
              dataSource.setUrl("jdbc:h2:~/devOps");
              dataSource.setUsername("sa");
              dataSource.setPassword("sa");
              return dataSource;
          }
  
          private Properties hibernateProperties() {
              System.out.println("inside hibernateProperties");
              Properties properties = new Properties();
              properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
              properties.put("hibernate.show_sql","true");
              properties.put("hibernate.format_sql","true");
              properties.put("hibernate.hbm2ddl.auto","update");
              return properties;        
          }
  
  
          @Bean(name="transactionManager")
          @Autowired
          public HibernateTransactionManager transactionManager(SessionFactory s) {
              System.out.println("inside transactionManager");
             HibernateTransactionManager txManager = new HibernateTransactionManager();
             txManager.setSessionFactory(s);
             return txManager;
          }
      }
  </code></pre>
  
  <p>AccountDaoImpl.java:</p>
  
  <pre><code>    package com.yusufjk.GrocersBasketBackend.daoImpl;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.yusufjk.GrocersBasketBackend.dao.AccountDao;
  import com.yusufjk.GrocersBasketBackend.domain.Account;
  
  
  @Repository(value ="userDao")
  @Transactional
  public class AccountDaoImpl implements AccountDao {
  
      @Autowired
      SessionFactory sessionFactory;
  
      public void InsertUser(Account account) {
          Session session=sessionFactory.openSession();
          Transaction transaction=session.getTransaction();
          transaction.begin();
          session.save(account);
          transaction.commit();
          session.close();        
      }
  
      public Account getUserById(int userId) {
  
          Session session=sessionFactory.openSession();
          Transaction transaction=session.getTransaction();
          transaction.begin();
          Account account = session.load(Account.class, userId);
          transaction.commit();
          session.close();
          return account;
          }
  
      public Account getUserByName(String userName) {
          Session session=sessionFactory.openSession();
          Transaction transaction=session.getTransaction();
          transaction.begin();
          Account account =session.load(Account.class, userName);
          transaction.commit();
          session.close();
          return account;
      }
  
      public Account getUserByEmail(String email) {
          Session session=sessionFactory.openSession();
          Transaction transaction=session.getTransaction();
          transaction.begin();
          Account account =session.load(Account.class, email);
          transaction.commit();
          session.close();
          return account;
          }
  
  }
  </code></pre>
  
  <p>These are the entity classes which might be the cause of the error:</p>
  
  <p>Product.java</p>
  
  <pre><code>    package com.yusufjk.GrocersBasketBackend.domain;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.Id;
  import javax.persistence.JoinTable;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Products")
  public class Product {
  
      private String pId;
      private String pName;
      private double price;
      private int quantity;
  
      public Product() {
      }
      @Id
      @Column(name = "Product_Code", length = 10, nullable = false)
      public String getpId() {
          return pId;
      }
      public void setpId(String pId) {
          this.pId = pId;
      }
      @Column(name = "Product_Name", length = 255, nullable = false)
      public String getpName() {
          return pName;
      }
      public void setpName(String pName) {
          this.pName = pName;
      }
      @Column(name = "Price", nullable = false)
      public double getPrice() {
          return price;
      }
      public void setPrice(double price) {
          this.price = price;
      }
      @Column(name = "Quantity", nullable = false)
      public int getQuantity() {
          return quantity;
      }
      public void setQuantity(int quantity) {
          this.quantity = quantity;
      }
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinTable(name="cid")
      private Category category;
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinTable(name="supplierID")
      private Supplier supplier;
  }
  </code></pre>
  
  <p>Category.java</p>
  
  <pre><code>    package com.yusufjk.GrocersBasketBackend.domain;
  
  import java.util.Set;
  
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  import org.springframework.stereotype.Component;
  
  @Component
  @Entity
  @Table
  public class Category {
  @Id
  @GeneratedValue
  private int cid;
  
  private String cname;
  
  @OneToMany(fetch=FetchType.LAZY,mappedBy="category")
  private Set&lt;Product&gt; product;
  
  public Set&lt;Product&gt; getProduct() {
      return product;
  }
  
  public void setProduct(Set&lt;Product&gt; product) {
      this.product = product;
  }
  
  public String getCname() {
      return cname;
  }
  
  public void setCname(String cname) {
  this.cname = cname;
  }
  
  public int getCid() {
      return cid;
  }
  
  public void setCid(int cid) {
      this.cid = cid;
  }
  }
  </code></pre>
  
  <p>But I am not creating the test case for either of them I don't know why it is showing error for these entities. I'm only creating the test case for Account entity so I'll share that as well.</p>
  
  <p>Account.java</p>
  
  <pre><code>  package com.yusufjk.GrocersBasketBackend.domain;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Table;
  import javax.validation.constraints.NotNull;
  
  import org.hibernate.validator.constraints.Email;
  
  @Entity
  @Table(name = "Accounts")
  public class Account{
        @Id 
        @Email
          private String uemail; 
          @GeneratedValue
          private int uid;
  
          @NotNull
          private String uname;
          @NotNull
          private String upassword;
  
          private String role;    
  
          public  String getRole() {
              return role;
          }
          public void setRole(String role) {
              this.role = role;
          }
          public String getUemail() {
              return uemail;
          }
          public void setUemail(String uemail) {
              this.uemail = uemail;
          }
          public int getUid() {
              return uid;
          }
          public void setUid(int uid) {
              this.uid = uid;
          }
          public String getUname() {
              return uname;
          }
          public void setUname(String uname) {
              this.uname = uname;
          }
          public String getUpassword() {
              return upassword;
          }
          public void setUpassword(String upassword) {
              this.upassword = upassword;
          }
  
  }
  </code></pre>