【问题标题】:Spring Boot 2.1 EntityManagerFactory Bean could not be found找不到 Spring Boot 2.1 EntityManagerFactory Bean
【发布时间】:2019-07-16 11:30:30
【问题描述】:

我有一个简单的带有 JPA、Web 和 PostgreSQL 的 Spring Boot 项目。我正在使用最新的 Spring Boot 版本 2.1.3.RELEASE

添加简单的 JpaRepository 应用程序后启动失败并出现以下错误:

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

Description:

Field dataMappingRepository in com.my.example.service.impl.SimpleServiceImpl 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.

我有一个简单的@Service 类,例如:

public class SimpleServiceImpl implements SimpleService {
  @Autowired private SimpleJpaRepository repo;
}

还有 JpaRepository:

public interface SimpleJpaRepository extends JpaRepository<SimpleEntity, Long> {}

这是我的 application.yml:

spring:
   datasource:
      url: jdbc:postgresql://localhost:5432/simple
      username: user
      password: pass
      driver-class-name: org.postgresql.Driver
   jpa:
      show-sql: false
      properties:
         hibernate:
            dialect: org.hibernate.dialect.PostgreSQLDialect
      hibernate:
         ddl-auto: validate

如果我将 spring-boot-starter-parent 更改为 2.0.8.RELEASE,应用程序将正确启动。

【问题讨论】:

  • 听起来 Hibernate 没有被自动配置。 Spring Boot 2.0.8 和 2.1.3 使用不同版本的 Hibernate(分别为 5.2.17.Final 和 5.3.7.Final),并且 5.3.7.Final 的 jar 之一在下载时可能已损坏。尝试从构建系统的缓存中删除 Hibernate,然后重新构建应用程序。
  • 你能显示你的 pom.xml 吗?
  • 你启用了@ComponentScan

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

好的,最后问题出在Andy Wilkinson 所说的缓存Hibernate 依赖项。

从我的 M2 存储库中删除 org.hibernate 文件夹并更新 maven 项目后,它可以正常工作。

【讨论】:

    【解决方案2】:

    我正在使用最新的 Spring Boot 版本 2.1.2.RELEASE。
    但是错误地,在属性标签中我忘记删除 4.3.5 版本,如下所示,这会导致加载的库之间出现混淆。
    &lt;properties&gt;&lt;hibernate.version&gt;4.3.5.Final&lt;/hibernate.version&gt;&lt;/properties&gt;
    在 org.hibernate 文件夹的 .m2 存储库中,我下载了两个版本库。 删除 org.hibernate 文件夹并更新 maven 项目。它会运行。

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 2011-12-23
      • 2018-07-03
      • 2018-04-02
      • 2022-11-10
      • 2017-10-23
      • 2018-12-13
      • 2018-10-01
      • 1970-01-01
      相关资源
      最近更新 更多