3

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)

Vadym Pechenoha
  • 574
  • 5
  • 17
Asker
  • 424
  • 3
  • 5
  • 16
  • So you exclude the configuration which configures the datasource and spa and you still expect that to be available... Just remove all annotations except the `@SpringBootApplication` annotation and restart the app. Also your `spring.jpa.database-platform` property is wrong, it has the wrong package name (there is a typo in the `hibernate` and `dialect`). – M. Deinum Jul 27 '17 at 12:22
  • I did this, but now it states: Cannot determine embedded database driver class for database type NONE – Asker Jul 27 '17 at 12:27
  • 1
    i had the same above issue 'Cannot determine embedded database driver class for database type NONE' and in my ide intellij it suggested to add the db in my jpa .... can u try doing that if not already done – Syed Anas Jul 27 '17 at 12:32
  • Sorry, what do you mean by adding the db in my jpa? How do you do that? did you solve the issue? – Asker Jul 27 '17 at 12:34
  • Fix your `spring.jpa.database-platform` property. The value should be something like `org.hibernate.dialect.MySQL57InnoDBDialect`. Your value is wrong. – M. Deinum Jul 27 '17 at 12:35
  • Did this, still same error – Asker Jul 27 '17 at 12:36
  • 1
    You should also have a datasource on your class path. You only have a driver. – M. Deinum Jul 27 '17 at 12:36
  • I tried looking this up, is there a way to just add this to the properties file without adding annotations? thanks! – Asker Jul 27 '17 at 12:40
  • Add what with annotations? You should have a datasource implementation on your class path, like HikariCP or Tomcat JDBC. Without that a datasource won't be auto configured. – M. Deinum Jul 27 '17 at 12:44
  • But how do I add that? I quickly looked it up and people use XML files, not annotations. I have only dependencies in my XML, but I am not sure how to set this up. – Asker Jul 27 '17 at 12:52
  • In my case, I removed the packages inside of src/test/java and my spring boot project start working after maven-install. – ArifMustafa Apr 16 '18 at 14:41

5 Answers5

7

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.

ryan2049
  • 361
  • 1
  • 8
  • I still get the error, even if I did what you said above... Cannot determine embedded database driver class for database type NONE – Asker Jul 28 '17 at 07:46
  • Is it possible for you to post your code base to GitHub? – ryan2049 Jul 28 '17 at 10:37
  • I will post it and post more questions here, the code still doesn't work, but now crashes once I do something with the database – Asker Jul 28 '17 at 10:47
  • I'm certain we can figure out what's wrong once we get a better look at it. Thanks – ryan2049 Jul 28 '17 at 10:48
  • https://github.com/MatejaSela/SpringBootEmployeeLogin Here is the project. Any help would be much obliged! – Asker Jul 28 '17 at 11:14
  • 1
    I have updated this answer to provide additional guidance. I also created a pull request against your repo. These updates should resolve your problems. – ryan2049 Jul 28 '17 at 20:00
0

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.

0

in my code,it was also happend like :

Consider defining a bean named 'entityManagerFactory' in your configuration.

when i change the version of hibernate.jar (pop.xml) from 4.. final to 5.. final. then it is ok. Maybe the main problem is the version of hibernate...

0

Check your versions: I had the same issue on "2.0.0.RELEASE" and updated it to the 2.4.2.
Now pom file includes this parent relation:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> 
</parent>

All other dependencies goes next:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Evan
  • 31
  • 2
0

In my case, I had downgraded Spring boot parent from 3.0.0 and 2.6.6. I had manually changed spring-boot-starter-data-jpa version to 3.0.1.

Spring boot autoconfiguration failed to load EntityManagerFactory and the application startup failed.

I believe spring-boot-starter-data-jpa 3.0.1 loads artifacts from org.hibernate.orm whereas auto configuration looks for org.hibernate.

Dependency management and the spring-boot-starter-data-jpa starter have been updated to use the new org.hibernate.orm group ID for their Hibernate dependencies.

Spring-Boot-3.0-Migration-Guide - Hibernate 6.1

Anyway, My issue got resolved as I downgraded the spring-boot-starter-data-jpa version also to 2.6.6.

BlackViper
  • 53
  • 5