【发布时间】: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