title: Configuring the SessionFactory in SpringBoot + Spring data JPA + MySql project
body: <p>I have been writing my first project with SpringBoot + Spring Data JPA. I use MySql as my database provider. I am sort of new to Spring Boot and Hibernate. </p>
  
  <p>I followed the tutorial available at <a href="http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/" rel="nofollow noreferrer">http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/</a> for my project.</p>
  
  <p>Everything is clear up until the session factory. The mentioned tutorial does not use a session factory; instead it uses <code>CrudRepository</code> based transactions. But in my case, since I am still new to Hibernate, I use SessionFactory for db operations. </p>
  
  <p>In my project, the DaoImpl class looks like follows.</p>
  
  <pre><code>@Repository
  public class ReadDaoImpl implements ReadDao{
  
      private static SessionFactory sessionFactory;
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      @Autowired
      public static void setSessionFactory(SessionFactory sessionFactory) {
          TenderReadDaoImpl.sessionFactory = sessionFactory;
      }
  
      @Override
      public List&lt;Read&gt; getAllReads() {
  
          System.out.println("DEBUG: Inside ReadDaoImpl.getAllReads");
  
          Session session = sessionFactory.openSession(); // I think that the issue comes from here
          Transaction tx = null;
          ..............................
          ..............................
      }
  }
  </code></pre>
  
  <p>My project starts running on the server without any issue. But when I access the relevant url for getAll() method, Eclipse console prints out a Null Pointer Exception referring to the following line.</p>
  
  <pre><code>Session session = sessionFactory.openSession();
  </code></pre>
  
  <p>I think that I miss some configurations for <code>sessionFactory</code>. But I cannot figure out what it is. </p>
  
  <p>Here is my <code>application.properties</code> file. It is in fact a copy of the same file from the above link.</p>
  
  <pre><code># ===============================
  # = DATA SOURCE
  # ===============================
  
  # Set here configurations for the database connection
  
  # Connection url for the database 
  spring.datasource.url = jdbc:mysql://localhost:3306/readsdb?useSSL=false
  
  # Username and password
  spring.datasource.username = root
  spring.datasource.password = password
  
  # Keep the connection alive if idle for a long time (needed in production)
  spring.datasource.testWhileIdle = true
  spring.datasource.validationQuery = SELECT 1
  
  # ===============================
  # = JPA / HIBERNATE
  # ===============================
  
  # Use spring.jpa.properties.* for Hibernate native properties (the prefix is
  # stripped before adding them to the entity manager).
  
  # Show or not log for each sql query
  spring.jpa.show-sql = true
  
  # Hibernate ddl auto (create, create-drop, update): with "update" the database
  # schema will be automatically updated accordingly to java entities found in
  # the project
  spring.jpa.hibernate.ddl-auto = create-drop
  
  # Naming strategy
  spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  
  # Allows Hibernate to generate SQL optimized for a particular DBMS
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  </code></pre>
  
  <p>In my project, there is no any configuration file. Do I need to add a one to create <code>sessionFactory</code> Bean? I came across something called <code>entityManagerFactory</code>. Do I need to configure the <code>sessionfactory</code> through that for this Spring Jpa based project? If so, How?</p>
  
  <p>I am completely out of solutions at the moment. Guide me to get this thing done. It would be much helpful for me if the exhaustive way of doing this can be pointed out.</p>
  
  <p>Thanks..!</p>