java - 为什么 Spring Boot 要求我定义 'entityManagerFactory' bean?

标签 java hibernate spring-boot

我正在尝试使用 spring boot 2.1.3 启动一个新应用程序,但收到此错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userProfileJpaRepository in ie.gtludwig.pa.service.impl.UserProfileServiceImpl required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

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


Process finished with exit code 0

我尝试删除 org.hibernate.hibernate-core我的 jar <user>/.m2/repository/org/hibernate我仍然遇到同样的错误。事实上,我已经删除了<user>/.m2/repository并重新安装了所有依赖项以确保仍然存在相同的问题。

我找到了this在 StackOverflow 上类似的线程上,它是通过删除 hibernate 依赖项解决的,但它对我不起作用。

IntelliJ 缓存也已清理。

有什么想法吗?

编辑 - 添加 UserProfileJpaRepository 和 pom.xml

UserProfileJpaRepository.java

public interface UserProfileJpaRepository extends JpaRepository<UserProfile, String> {

    UserProfile findByType(String type);
}

pom.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>ie.gtludwig.pa</groupId>
    <artifactId>pa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>PA</name>
    <modules>
        <module>core</module>
        <module>engine</module>
    </modules>

    <properties>
        <!-- JAVA -->
        <version.java>1.8</version.java>
        <java.version>${version.java}</java.version>
        <jdk.version>${version.java}</jdk.version>
        <maven.compiler.target>${version.java}</maven.compiler.target>
        <maven.compiler.source>${version.java}</maven.compiler.source>
        <!-- Generic properties -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <!-- External Dependency Versions -->
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <spring-boot-jdbc.version>2.1.3.RELEASE</spring-boot-jdbc.version>
        <spring-boot-data-redis.version>2.1.3.RELEASE</spring-boot-data-redis.version>
        <jedis.version>2.9.1</jedis.version>
        <junit.version>4.12</junit.version>
        <logback.version>1.2.3</logback.version>
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
        <flyway.version>5.2.0</flyway.version>
        <mysql.version>8.0.16</mysql.version>
        <h2database.version>1.4.199</h2database.version>
        <sendgrid-java.version>4.3.0</sendgrid-java.version>
        <commons-lang3.version>3.7</commons-lang3.version>
        <commons-io.version>2.6</commons-io.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <!--WEB-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>${thymeleaf-layout-dialect.version}</version>
        </dependency>

        <!--SECURITY-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-jose</artifactId>
        </dependency>

        <!--OPERATIONS-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--            <version>${spring-boot-data-redis.version}</version>-->
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>io.lettuce</groupId>-->
<!--                    <artifactId>lettuce-core</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>redis.clients</groupId>-->
<!--            <artifactId>jedis</artifactId>-->
<!--            <version>${jedis.version}</version>-->
<!--        </dependency>-->

         <!--SERVER-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-mail</artifactId>-->
<!--        </dependency>-->

        <!--DATABASE-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>${spring-boot-jdbc.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.flywaydb</groupId>-->
            <!--<artifactId>flyway-core</artifactId>-->
            <!--<version>${flyway.version}</version>-->
        <!--</dependency>-->
        <!--TEST DATABASE FOR TESTING PROCESSES AND RULES IN-MEMORY-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2database.version}</version>
        </dependency>

        <!--EMAIL-->
        <!-- https://mvnrepository.com/artifact/com.sendgrid/sendgrid-java -->
        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>sendgrid-java</artifactId>
            <version>${sendgrid-java.version}</version>
        </dependency>

        <!--DEV/TEST-->
        <!--DEV TOOLS CONFLICT WITH JREBEL - DISABLE THIS IF JREBEL IS BEING USED -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>5.0.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- Resource plugin to enable expanding properties from this file so that they can be exposed by the zone (E.g. @project.version@) -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <nonFilteredFileExtensions>
                        <!--font/binary files must be excluded from filtering or they will be corrupted-->
                        <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <!-- Add our project version to the manifest file -->
                            <version>${project.version}</version>
                            <description>${project.description}</description>
                        </manifestEntries>
                    </archive>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <attachClasses>false</attachClasses>
                </configuration>
                <version>3.1.0</version>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>external</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <version>${spring-boot.version}</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

最佳答案

看来您在实现中注入(inject)了 EntityManagerFactory
要获取 EntityManagerFactory ,您需要执行以下操作:

@PersistenceContext
private EntityManager entityManager;

// in code
EntityManagerFactory factory = entityManager.getEntityManagerFactory();

关于java - 为什么 Spring Boot 要求我定义 'entityManagerFactory' bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56116139/

相关文章:

postgresql - 从 Azure 连接到 Amazon RDS 上的 Postgres 数据库时超时

java - 任何用于查找 java 代码中的 jdbc 连接泄漏的 eclipse 插件

eclipse - org.hibernate.InvalidMappingException : Could not parse mapping document from resource *. hbm.xml

java - org.hibernate.exception.SQLGrammarException : ORA-01747: invalid user. table.column、table.column 或列规范

java - 使用 cglib 或 javaassist 哪一个

java - Springboot @ServerEndPoint "Failed to find the root WebApplicationContext."

java - JPA 和 Lombok 在循环依赖中导致 StackOverflowError

java - Game Of Life Java - 使用二维数组和 for 循环计算邻居

java - 如何停止使用 Eclipse 外部工具配置启动的进程

java - JPA : Generating Data Transfer Object DTO from Entity and merging DTO to database 的模式