title: Hibernate configuration for Mysql 8.0
body: <p>I'm new in Java! When I run the Junit test the console show me exception and I don't know how to resolve this. What is wrong with my Hibernate configuration? This code perfect works with H2 database, but when I change database and config parameters to Mysql 8.0 the Eclipse return exception. I hope that someone can give me useful advice and explain how resolve this problem. Thanks.  </p>
  
  <p>This is my Hibernate Configuration for Mysql 8.0</p>
  
  <pre><code>import java.util.Properties;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.dbcp2.BasicDataSource;
  import org.hibernate.SessionFactory;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.orm.hibernate5.HibernateTransactionManager;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  @Configuration
  @ComponentScan(basePackages= {"com.savaleks.websiteback.dto"})
  @EnableTransactionManagement
  public class HibernateConfig {
  
      private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/website";
      private final static String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
      private final static String DATABASE_DIALECT = "org.hibernate.dialect.MySQL8Dialect";
      private final static String DATABASE_USERNAME = "root";
      private final static String DATABASE_PASSWORD = "";
  
      @Bean
      public DataSource getDataSource() {
  
          BasicDataSource dataSource = new BasicDataSource();
  
          // Database connection information
          dataSource.setDriverClassName(DATABASE_DRIVER);
          dataSource.setUrl(DATABASE_URL);
          dataSource.setUsername(DATABASE_USERNAME);
          dataSource.setPassword(DATABASE_PASSWORD);
  
          return dataSource;
      }
  
      @Bean
      public SessionFactory getSessionFactory(DataSource dataSource) {
  
          LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
  
          builder.addProperties(getHibernateProperties());
          builder.scanPackages("com.savaleks.websiteback.dto");
  
          return builder.buildSessionFactory();
      }
  
      // All Hibernate properties returned in this method
      private Properties getHibernateProperties() {
  
          Properties properties = new Properties();
          properties.put("hibernate.dialect", DATABASE_DIALECT);
          properties.put("hibernate.show_sql", "true");
          properties.put("hibernate.format_sql", "true");
  
          return null;
      }
  
      @Bean
      public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
  
          HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
          return transactionManager;
  
      }
  }
  </code></pre>
  
  <p>And this is console output</p>
  
  <pre><code>Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryDAO': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.savaleks.websiteback.config.HibernateConfig: 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.NullPointerException
  </code></pre>
  
  <p>pom.xml file</p>
  
  <pre><code>&lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;spring.version&gt;4.3.20.RELEASE&lt;/spring.version&gt;
          &lt;mysql.version&gt;8.0.13&lt;/mysql.version&gt;
          &lt;hibernate.version&gt;5.2.17.Final&lt;/hibernate.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
          &lt;!-- JUNIT TEST --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.12&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- SPRING CONTEXT --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- MYSQL DATABASE --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysql.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- HIBERNATE --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- DATABASE CONNECTION POOLING --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
              &lt;version&gt;2.5.0&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
              &lt;version&gt;1.4&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>If need more information, I can load more classes and console output. What could be wrong? any ideas?</p>