【问题标题】:Spring Boot Hibernate throws constraint warning on first startup with empty databaseSpring Boot Hibernate 在第一次使用空数据库启动时抛出约束警告
【发布时间】:2020-05-15 11:49:19
【问题描述】:

我有一个 Spring Boot 应用程序,我想创建带有实体注释的表。当我运行 jar 时,如果表已经存在,应用程序会在没有警告的情况下启动。 但是如果数据库是空的,那么我会为每个表收到这些警告:

GenerationTarget encountered exception accepting command : Error executing DDL "alter table mytable drop constraint if exists FKk53mrkxu32slsax6fmn164ijy" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table mytable drop constraint if exists FKk53mrkxu32slsax6fmn164ijy" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final] (....)

还有:

2020-05-15 11:23:24.998  WARN 20319 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 0, SQLState: 00000
2020-05-15 11:23:24.998  WARN 20319 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : table "mytable" does not exist, skipping

即使出现这些警告,应用程序也能正常工作,那么有没有办法解决问题或抑制警告?

我的 application.properties 数据库相关行:

spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
spring.datasource.username=user
spring.datasource.password=user
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.initialize=true

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=false

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

更新: 第一个较长的异常仅在表具有外键并且异常的结尾如下所示时发生:

Caused by: org.postgresql.util.PSQLException: ERROR: relation "mytable" does not exist

Caused by: org.postgresql.util.PSQLException: ERROR: relation "student_x_course" does not exist

出现异常后,Hibernate 运行 CREATE TABLE ... sql 语句。

【问题讨论】:

    标签: java hibernate spring-boot jpa


    【解决方案1】:

    我将此添加到我的 application.properties 以隐藏警告:

    logger.org.hibernate=error
    

    【讨论】:

      【解决方案2】:

      尝试验证您的表“mytable”是否存在,我认为您必须更改

      spring.jpa.hibernate.ddl-auto=create
      

      spring.jpa.hibernate.ddl-auto=update
      

      【讨论】:

      • 我想使用 import.sql,它只适用于 create 或 create-drop。
      【解决方案3】:

      尝试添加spring.datasource.initialization-mode=always。 或者通过脚本初始化数据库。在 resources 文件夹中添加带有创建脚本的 schema.sql 文件,并将 ddl-auto 更改为 none。

      【讨论】:

        猜你喜欢
        • 2014-09-03
        • 2023-04-06
        • 1970-01-01
        • 2017-04-14
        • 2021-08-31
        • 1970-01-01
        • 2016-08-07
        • 1970-01-01
        相关资源
        最近更新 更多