title: Caused by: java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found
body: <p>I am following the same code from the link : <a href="http://websystique.com/spring-boot/spring-boot-angularjs-spring-data-jpa-crud-app-example/" rel="nofollow noreferrer">http://websystique.com/spring-boot/spring-boot-angularjs-spring-data-jpa-crud-app-example/</a>. But when I simply run the main method of <code>Spring Boot</code>, I see that the below error is coming. Could you please guide? I am using <code>Java 7</code>.</p>
  
  <pre><code> org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/websystique/springboot/configuration/JpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found
          at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1134) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1028) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
          at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar:1.4.3.RELEASE]
          at com.websystique.springboot.SpringBootCRUDApp.main(SpringBootCRUDApp.java:15) ~[classes/:na]
      Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          ... 18 common frames omitted
      Caused by: java.lang.IllegalStateException: required key [datasource.sampleapp.hibernate.dialect] not found
          at org.springframework.core.env.AbstractPropertyResolver.getRequiredProperty(AbstractPropertyResolver.java:183) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.core.env.AbstractEnvironment.getRequiredProperty(AbstractEnvironment.java:556) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at com.websystique.springboot.configuration.JpaConfiguration.jpaProperties(JpaConfiguration.java:98) ~[classes/:na]
          at com.websystique.springboot.configuration.JpaConfiguration.entityManagerFactory(JpaConfiguration.java:80) ~[classes/:na]
          at com.websystique.springboot.configuration.JpaConfiguration$$EnhancerBySpringCGLIB$$3ce60e74.CGLIB$entityManagerFactory$2(&lt;generated&gt;) ~[classes/:na]
          at com.websystique.springboot.configuration.JpaConfiguration$$EnhancerBySpringCGLIB$$3ce60e74$$FastClassBySpringCGLIB$$a174a592.invoke(&lt;generated&gt;) ~[classes/:na]
          at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          at com.websystique.springboot.configuration.JpaConfiguration$$EnhancerBySpringCGLIB$$3ce60e74.entityManagerFactory(&lt;generated&gt;) ~[classes/:na]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25]
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25]
          at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]
          ... 19 common frames omitted
  </code></pre>
  
  <p><strong>application.yml</strong></p>
  
  <pre><code>---
  server:
    port: 8080
    contextPath: /SpringBootCRUDApp
  ---
  spring:
    profiles: local
  datasource:
    sampleapp:
      url: jdbc:h2:~/test
      username: SA
      password:
      driverClassName: org.h2.Driver
      defaultSchema:
      maxPoolSize: 10
      hibernate:
        hbm2ddl.method: create-drop
        show_sql: true
        format_sql: true
        dialect: org.hibernate.dialect.H2Dialect
  ---
  spring:
    profiles: prod
  datasource:
    sampleapp:
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: root
      driverClassName: com.mysql.jdbc.Driver
      defaultSchema:
      maxPoolSize: 20
      hibernate:
        hbm2ddl.method: update
        show_sql: true
        format_sql: true
        dialect: org.hibernate.dialect.MySQLDialect
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.4.3.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.7&lt;/java.version&gt;
          &lt;h2.version&gt;1.4.187&lt;/h2.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;!-- Add typical dependencies for a web application --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Add freemarker template support --&gt;        
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-freemarker&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Add JPA support --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Add Hikari Connection Pooling support --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
              &lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Add H2 database support [for running with local profile] --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&lt;/artifactId&gt;
              &lt;version&gt;${h2.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Add MySQL database support [for running with PRODUCTION profile] --&gt;       
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
              &lt;version&gt;3.5&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
  
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;&lt;!-- Include if you want to make an executable jar[FAT JAR which 
                      includes all dependencies along with sprinboot loader] that you can run on 
                      commandline using java -jar NAME --&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  </code></pre>
  
  <p><strong>SpringBootCRUDApp.java</strong></p>
  
  <pre><code>@Import(JpaConfiguration.class)
  @SpringBootApplication(scanBasePackages={"com.websystique.springboot"})// same as @Configuration @EnableAutoConfiguration @ComponentScan
  public class SpringBootCRUDApp {
  
      public static void main(String[] args) {
          SpringApplication.run(SpringBootCRUDApp.class, args);
      }
  }
  </code></pre>
  
  <p><strong>JpaConfiguration.java</strong></p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(basePackages = "com.websystique.springboot.repositories",
  entityManagerFactoryRef = "entityManagerFactory",transactionManagerRef = "transactionManager")
  @EnableTransactionManagement
  public class JpaConfiguration {
  
      @Autowired
      private Environment environment;
  
      @Value("${datasource.sampleapp.maxPoolSize:10}")
      private int maxPoolSize;
  
      /*
       * Populate SpringBoot DataSourceProperties object directly from application.yml 
       * based on prefix.Thanks to .yml, Hierachical data is mapped out of the box with matching-name
       * properties of DataSourceProperties object].
       */
      @Bean
      @Primary
      @ConfigurationProperties(prefix = "datasource.sampleapp")
      public DataSourceProperties dataSourceProperties(){
          return new DataSourceProperties();
      }
  
      /*
       * Configure HikariCP pooled DataSource.
       */
      @Bean
      public DataSource dataSource() {
          DataSourceProperties dataSourceProperties = dataSourceProperties();
          HikariDataSource dataSource = (HikariDataSource) DataSourceBuilder
                  .create(dataSourceProperties.getClassLoader())
                  .driverClassName(dataSourceProperties.getDriverClassName())
                  .url(dataSourceProperties.getUrl())
                  .username(dataSourceProperties.getUsername())
                  .password(dataSourceProperties.getPassword())
                  .type(HikariDataSource.class)
                  .build();
          dataSource.setMaximumPoolSize(maxPoolSize);
          return dataSource;
      }
  
      /*
       * Entity Manager Factory setup.
       */
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
          factoryBean.setDataSource(dataSource());
          factoryBean.setPackagesToScan(new String[] { "com.websystique.springboot.model" });
          factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
          factoryBean.setJpaProperties(jpaProperties());
          return factoryBean;
      }
  
      /*
       * Provider specific adapter.
       */
      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
          HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
          return hibernateJpaVendorAdapter;
      }
  
      /*
       * Here you can specify any provider specific properties.
       */
      private Properties jpaProperties() {
          Properties properties = new Properties();
          properties.put("hibernate.dialect", environment.getRequiredProperty("datasource.sampleapp.hibernate.dialect"));
          properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("datasource.sampleapp.hibernate.hbm2ddl.method"));
          properties.put("hibernate.show_sql", environment.getRequiredProperty("datasource.sampleapp.hibernate.show_sql"));
          properties.put("hibernate.format_sql", environment.getRequiredProperty("datasource.sampleapp.hibernate.format_sql"));
  
          if(StringUtils.isNotEmpty(environment.getRequiredProperty("datasource.sampleapp.defaultSchema"))){
              properties.put("hibernate.default_schema", environment.getRequiredProperty("datasource.sampleapp.defaultSchema"));
          }
          return properties;
      }
  
  
      @Bean
      @Autowired
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
          JpaTransactionManager txManager = new JpaTransactionManager();
          txManager.setEntityManagerFactory(emf);
          return txManager;
      }
  }
  </code></pre>