【问题标题】:Spring Boot 2.3 fails to start when spring.jpa.hibernate.ddl-auto equals to validatespring.jpa.hibernate.ddl-auto 等于验证时 Spring Boot 2.3 无法启动
【发布时间】:2021-03-13 08:10:46
【问题描述】:

我有一个 Spring boot: 2.3.0.RELEASE 应用程序,带有 Flyway: 6.4.1 和 Hibernate: hibernate-core: 5.4.22.Final, hibernate-validator: 6.1.5.Final, hibernate-commons-annotations: 5.1.0.决赛。

我尝试搜索错误但找不到解决方案。我尝试应用这个answer,但每当设置了spring.jpa.hibernate.ddl-auto=validate 时,如果我使用nonedrop-create 将一切都恢复为正常值,它就不起作用了。

我在 Docker 中运行 MySQL 5.7,带有表的数据库就在那里。

错误如下:

ConfigServletWebServerApplicationContext : 遇到异常 在上下文初始化期间 - 取消刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建定义名称为“requestMappingHandlerAdapter”的 bean 时出错 在类路径资源中 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 通过方法表达的不满足的依赖关系 'requestMappingHandlerAdapter' 参数 1;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 使用在类路径中定义的名称“mvcConversionService”创建 bean 资源 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.format.support.FormattingConversionService]: 工厂方法“mvcConversionService”抛出异常;嵌套的 例外是 org.springframework.beans.factory.BeanCreationException: 创建名称为“customerRepository”的 bean 时出错 org.cloudwheel.files.configuration.customer.persistence.CustomerRepository 在声明的@EnableJpaRepositories 中定义 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration:不能 在设置 bean 时解析对 bean 'jpaMappingContext' 的引用 属性“映射上下文”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“jpaMappingContext”的 bean:调用 init 方法 失败的;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] 无法构建 Hibernate SessionFactory; 嵌套异常是 org.hibernate.tool.schema.spi.SchemaManagementException: 架构验证:缺少表 [my_files]

我该如何调试呢?我看到很多这样的问题,但没有一个解决方案对我有用。

注意:表 my_files 存在。我可以通过 IntelliJ 和 MySQL Workbench 验证这一点。另外,如果可能的话,我不想降级。

更新:

我确信数据库不是问题:

2020-12-01 14:24:04.795 信息 23295 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat 初始化为 端口:8080 (http) 2020-12-01 14:24:04.800 信息 23295 --- [ restartedMain] o.apache.catalina.core.StandardService : 开始 服务 [Tomcat] 2020-12-01 14:24:04.800 信息 23295 --- [ restartedMain] org.apache.catalina.core.StandardEngine:开始 Servlet 引擎:[Apache Tomcat/9.0.35] 2020-12-01 14:24:04.895 INFO 23295 --- [restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]: 初始化 Spring 嵌入式 WebApplicationContext 2020-12-01 14:24:04.895 信息 23295 --- [restartedMain] os.web.context.ContextLoader:根 WebApplicationContext: 初始化在 1762 毫秒内完成 2020-12-01 14:24:07.982 INFO 23295 --- [restartedMain] o.f.c.internal.license.VersionPrinter: Flyway 社区版 6.4.1 由 Redgate 发布 2020-12-01 14:24:07.986 信息 23295 --- [restartedMain] com.zaxxer.hikari.HikariDataSource
: HikariPool-1 - 开始... 2020-12-01 14:24:08.090 INFO 23295 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1

  • 启动完成。 2020-12-01 14:24:08.113 INFO 23295 --- [restartedMain] o.f.c.internal.database.DatabaseFactory:数据库: jdbc:mysql://localhost:3307/files (MySQL 5.7) 2020-12-01 14:24:08.186 信息 23295 --- [restartedMain] o.f.core.internal.command.DbValidate : 成功验证了 13 个迁移(执行时间 00:00.029s) 2020-12-01 14:24:08.199 信息 23295 --- [restartedMain] o.f.core.internal.command.DbMigrate :架构的当前版本 files: 1.13 2020-12-01 14:24:08.200 信息 23295 --- [ restartedMain] o.f.core.internal.command.DbMigrate :架构 files 是最新的。无需迁移。 2020-12-01 14:24:08.268 信息 23295 --- [restartedMain] osss.concurrent.ThreadPoolTask​​Executor:初始化 ExecutorService 'applicationTaskExecutor' 2020-12-01 14:24:08.281 信息 23295 --- [restartedMain] os.s.c.ThreadPoolTask​​Scheduler
    : 初始化 ExecutorService 'taskScheduler' 2020-12-01 14:24:08.330 信息 23295 --- [任务 1] o.hibernate.jpa.internal.util.LogHelper:HHH000204:处理中 PersistenceUnitInfo [名称:默认] 2020-12-01 14:24:08.374 INFO 23295 --- [task-1] org.hibernate.Version: HHH000412:Hibernate ORM 核心版本 5.4.22.Final

【问题讨论】:

  • 它在抱怨缺少表不是关于缺少数据库。
  • @M.Deinum,我修正了错字。我的意思是表,而不是数据库。
  • 不管你怎么想,桌子都不存在。您可能正在查看与您的应用程序不同的数据库。
  • @M.Deinum 为什么除了validate 之外的所有其他选项都可以使用?
  • 因为不验证。它会忽略所有内容(none)或更改架构(create)。

标签: java spring spring-boot hibernate spring-mvc


【解决方案1】:

例外是org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [my_files]

您说数据库 my_files 存在,但 hibernate 需要一个表 my_files 存在 - 例如您使用注释 @Table(name = "my_files") 或者您有名为 MyFiles 的实体。

检查数据库是否包含表my_files

【讨论】:

  • 我使用 Kotlin,而不是 Java,但我的代码中有这个实体:@Entity @Table(name = "my_files") data class MyFile
【解决方案2】:

正如@M.Deinum 在他的 cmets Flyway 和 Hibernate 中提到的那样,可能没有连接到同一个数据源。就我而言,这是因为hibernate.temp.use_jdbc_metadata_defaults=true。一旦我删除了这个属性,一切都开始工作了。

【讨论】:

    猜你喜欢
    • 2018-11-13
    • 2021-04-09
    • 2017-12-25
    • 1970-01-01
    • 2015-12-15
    • 2018-04-06
    • 2021-04-18
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多