java 在 SpringBoot 中找不到 EntityManagerFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45350546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:39:31  来源:igfitidea点击:

EntityManagerFactory not found in SpringBoot

javamysqlspring-boot

提问by Asker

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:

我正在尝试使用 MySQL 和 SpringBoot 为员工创建登录名。我使用内存数据库使我的代码工作,但是一旦我将代码切换到 MySQL,它就停止工作。这个项目是一个科学怪人,所以我不确定我的一些组件是否可以一起工作。我不确定是否需要我的 App 类中的所有注释......错误如下:

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.

考虑定义一个'entityManagerFactory'在您的配置中命名的 bean 。

Here is my Appclass, that runs the whole thing:

这是我的App课程,它运行整个过程:

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 文件和属性文件:

<?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)

entityManager 应该在哪里?考虑到我的项目目前非常简单(有服务、控制、员工和应用程序类),我应该怎么做

回答by ryan2049

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

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL从您的EmployeeApiDataApplication班级中删除此行。

The @SpringBootApplicationannotation implicitly includes the @EnableAutoConfiguration. Also DataSourceAutoConfigurationis necessary for spring-boot to instantiate a DataSourceand HibernateJpaAutoConfigurationis necessary to instantiate the EntityManagerFactory.

@SpringBootApplication注释隐含包括@EnableAutoConfiguration。也DataSourceAutoConfiguration有必要春季启动实例化DataSourceHibernateJpaAutoConfiguration需要来实例化EntityManagerFactory

It looks like you have included the mysql:mysql-connector-javadependency 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.

看起来您已经包含了mysql:mysql-connector-java依赖项,这很好。如果您的项目是最近添加的,请确保您已对其进行了 maven 更新,以便将其添加到您的类路径中。

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.Driverdepends on the mysql:mysql-connector-javadependency.

com.mysql.jdbc.Driver取决于mysql:mysql-connector-java依赖。

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

还要确保create-drop是您想要的正确设置spring.jpa.hibernate.ddl-auto。您可以在此处找到可能的值https://stackoverflow.com/a/1689769/

UPDATE (after git inspection):

更新(经过 git 检查):

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

  2. I updated application.propertiesto reflect the changes notated above.

  3. I removed the methods from UserRepositoryand updated UserServiceto handle those changes.

  1. 我删除了反对EmployeeApiDataApplication只留下的附加注释@SpringBootApplication。除非您确实有理由,否则您在这里不需要如此多的特殊性。

  2. 我更新application.properties以反映上述更改。

  3. 我删除了这些方法UserRepository并进行了更新UserService以处理这些更改。

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

我在这里创建了一个拉取请求https://github.com/MatejaSela/SpringBootEmployeeLogin/pull/1

Hope this helps.

希望这可以帮助。

回答by Santosh Anantharamaiah

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

根据您的问题,我了解您正在寻求实现基本的 Spring Boot JPA 集成。

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

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.

在这个例子中清楚地解释了每个非常单独的步骤。

回答by user9814979

in my code,it was also happend like :

在我的代码中,它也发生在:

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

考虑在您的配置中定义一个名为“entityManagerFactory”的 bean。

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...

当我将 hibernate.jar (pop.xml) 的版本从 4. . 最终5 .. 最后。那么没关系。也许主要问题是hibernate的版本...