title: UnsatisfiedDependencyException when using Hibernate with H2 database in Spring Core Maven Project
body: <blockquote>
    <p>Exception encountered during context initialization - cancelling
    refresh attempt:
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'categoryDAO' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Unsatisfied
    dependency expressed through method 'getCategoryDao' parameter 0:
    Error creating bean with name 'sessionFactory' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Bean
    instantiation via factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.hibernate.SessionFactory]: Factory method
    'getSessionFactory' threw exception; nested exception is
    java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index;; nested
    exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'sessionFactory' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Bean
    instantiation via factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.hibernate.SessionFactory]: Factory method
    'getSessionFactory' threw exception; nested exception is
    java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index; Exception
    in thread "main"
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'categoryDAO' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Unsatisfied
    dependency expressed through method 'getCategoryDao' parameter 0:
    Error creating bean with name 'sessionFactory' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Bean
    instantiation via factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.hibernate.SessionFactory]: Factory method
    'getSessionFactory' threw exception; nested exception is
    java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index;; nested
    exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'sessionFactory' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Bean
    instantiation via factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.hibernate.SessionFactory]: Factory method
    'getSessionFactory' threw exception; nested exception is
    java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index;   at
    org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
      at
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      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:776)
      at
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
      at
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
      at com.niit.shoppingcart.CategoryTest.main(CategoryTest.java:13)
    Caused by: org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'sessionFactory' defined in
    com.niit.shoppingcart.config.ApplicationContextConfig: Bean
    instantiation via factory method failed; nested exception is
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.hibernate.SessionFactory]: Factory method
    'getSessionFactory' threw exception; nested exception is
    java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index;   at
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      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:207)
      at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1214)
      at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
      at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
      at
    org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
      at
    org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
      ... 13 more Caused by:
    org.springframework.beans.BeanInstantiationException: Failed to
    instantiate [org.hibernate.SessionFactory]: Factory method
    'getSessionFactory' threw exception; nested exception is
    java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index;   at
    org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
      at
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
      ... 27 more Caused by: java.lang.NoSuchMethodError:
    javax.persistence.Table.indexes()[Ljavax/persistence/Index;   at
    org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1106)
      at
    org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:774)
      at
    org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
      at
    org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
      at
    org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
      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:723)
      at
    org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
      at
    com.niit.shoppingcart.config.ApplicationContextConfig.getSessionFactory(ApplicationContextConfig.java:56)
      at
    com.niit.shoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$449f4899.CGLIB$getSessionFactory$1()
      at
    com.niit.shoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$449f4899$$FastClassBySpringCGLIB$$7c760c88.invoke()
      at
    org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      at
    org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
      at
    com.niit.shoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$449f4899.getSessionFactory()
      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:498)     at
    org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
      ... 28 more</p>
  </blockquote>
  
  <p><strong>This is ApplicationContextConfig.java file</strong></p>
  
  <pre><code> package com.niit.shoppingcart.config;
  
      import java.util.Properties;
  
      import javax.sql.DataSource;
  
      import org.apache.commons.dbcp2.BasicDataSource;
      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.orm.hibernate4.HibernateTransactionManager;
      import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
      import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  
      import com.niit.shoppingcart.dao.CategoryDAO;
      import com.niit.shoppingcart.dao.CategoryDAOImpl;
      import com.niit.shoppingcart.model.Category;
      import com.niit.shoppingcart.model.Product;
      import com.niit.shoppingcart.model.Supplier;
      import com.niit.shoppingcart.model.UserDetails;
  
  
      @Configuration
      @ComponentScan("com.niit.shoppingcart")
      @EnableTransactionManagement
      public class ApplicationContextConfig {
  
          @Bean(name = "dataSource")
          public DataSource getDataSource() {
              BasicDataSource dataSource = new BasicDataSource();
              dataSource.setDriverClassName("org.h2.Driver");
              dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
              dataSource.setUsername("sa");
              dataSource.setPassword("");
  
              return dataSource;
          }
  
  
          private Properties getHibernateProperties() {
              Properties properties = new Properties();
              properties.put("hibernate.hbm2ddl.auto","update");
              properties.put("hibernate.show_sql", "true");
              properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
              return properties;
          }
  
          @Autowired
          @Bean(name = "sessionFactory")
          public SessionFactory getSessionFactory(DataSource dataSource) {
              LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
              sessionBuilder.addProperties(getHibernateProperties());
              sessionBuilder.addAnnotatedClasses(Category.class);
              sessionBuilder.addAnnotatedClasses(Supplier.class);
              sessionBuilder.addAnnotatedClasses(UserDetails.class);
              sessionBuilder.addAnnotatedClasses(Product.class);
              return sessionBuilder.buildSessionFactory();
          }
  
          @Autowired
          @Bean(name = "transactionManager")
          public HibernateTransactionManager getTransactionManager(
                  SessionFactory sessionFactory) {
              HibernateTransactionManager transactionManager = new HibernateTransactionManager(
                      sessionFactory);
  
              return transactionManager;
          }
  
          @Autowired
          @Bean(name = "categoryDAO")
          public CategoryDAO getCategorDAO(SessionFactory sessionFactory) {
              return new CategoryDAOImpl(sessionFactory);
          }
      }
  </code></pre>
  
  <p><strong>This is the java test file in which the exception is occurring</strong></p>
  
  <pre><code>package com.niit.shoppingcart;
  
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  import com.niit.shoppingcart.dao.CategoryDAO;
  import com.niit.shoppingcart.model.Category;
  
  public class CategoryTest {
  
      public static void main(String[] args) {
          AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
          context.scan("com.niit.shoppingcart");
          context.refresh();
          CategoryDAO categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
  
          Category category = (Category) context.getBean("category");
  
          category.setC_id("cg1234");
          category.setC_name("phone");
          category.setC_desc("This is the first category");
  
          if (categoryDAO.save(category) == true) {
              System.out.println("Category created");
          } else {
              System.out.println("Unable to create category");
          }
      }
  }
  </code></pre>