EntityManagerFactory not found in SpringBoot

I am trying to create a login for employees, using MySQL and SpringBoot. I made my code work using an in-memory Database, but as soon as I switched my code to MySQL, it stopped working. This project is a Frankenstein, so I am not sure whether some of my components can work together. I am not sure whether all the annotations in my App class are needed… The error is the following:

Description:

Field employeeRepository in io.msela.springbootstarter.employee.EmployeeService 
required a bean named 'entityManagerFactory' that could not be found.

Action:

Consider defining a bean named ‘entityManagerFactory’ in your configuration.

Here is my App class, that runs the whole thing:

package io.msela;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import io.msela.springbootstarter.employee.EmployeeRepository;

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL
@ComponentScan({"io.msela"})
@EntityScan("io.msela.springbootstarter")
@EnableJpaRepositories(basePackageClasses = EmployeeRepository.class)
public class EmployeeApiDataApplication {

    public static void main(String[] args) {
        SpringApplication.run(EmployeeApiDataApplication.class, args);
    }
}

And here is the XML file and the properties file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.msela</groupId>
    <artifactId>course-api-data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>course-api-data</name>
    <description>Course API with Spring Data</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <!-- <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency> -->


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

And properies:

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.jpa.database-platform = org.hiberante.dialetct.MySQLSDialect
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

Where should the entityManager be? And how should I make it considering my project is very simple for now (has a service, control, employee and app classes)

Remove this line @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL from your EmployeeApiDataApplication class.

The @SpringBootApplication annotation implicitly includes the @EnableAutoConfiguration. Also DataSourceAutoConfiguration is necessary for spring-boot to instantiate a DataSource and HibernateJpaAutoConfiguration is necessary to instantiate the EntityManagerFactory.

It looks like you have included the mysql:mysql-connector-java dependency and that is good. Make sure you have done a maven update on your project if it was recently added so that it will be added to your classpath.

Your properties should look like this

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

com.mysql.jdbc.Driver depends on the mysql:mysql-connector-java dependency.

Also make sure create-drop is the correct setting you want on spring.jpa.hibernate.ddl-auto. You can find the possible values here https://stackoverflow.com/a/1689769/.

UPDATE (after git inspection):

  1. I removed the additional annotations against EmployeeApiDataApplication leaving only @SpringBootApplication. You don’t need so much specificity here unless you actually have a reason for it.

  2. I updated application.properties to reflect the changes notated above.

  3. I removed the methods from UserRepository and updated UserService to handle those changes.

I created a pull request here https://github.com/MatejaSela/SpringBootEmployeeLogin/pull/1

Hope this helps.

Based on your question, I understand, you are looking for achieving the basic Spring Boot JPA integration.

http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html

This is the best working example what you could leverage and customize according to your requirements.

Each and very individual steps are clearly explained in this example.