【问题标题】:Spring Boot with Hibernate generating drop constraint errors on startup with H2 database使用 Hibernate 的 Spring Boot 在使用 H2 数据库启动时生成丢弃约束错误
【发布时间】:2014-09-03 20:13:39
【问题描述】:

我正在使用 spring-boot 并且有一个这样配置的 H2 数据库(在 application.properties 中)。

spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

在日志中我看到以下错误:

o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.5.Final}
org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table key_request drop constraint FK_53shrbc21c25inskpp1yoxxss if exists
org.hibernate.tool.hbm2ddl.SchemaExport  : Table "KEY_REQUEST" not found; SQL statement:
    alter table key_request drop constraint FK_53shrbc21c25inskpp1yoxxss if exists [42102-178]

即使 hibernate 将这些报告为错误,我也可以登录到 H2 控制台并查看约束,它们看起来还不错。

SELECT TABLE_NAME, INDEX_NAME, COLUMN_NAME, INDEX_TYPE_NAME FROM information_schema.indexes WHERE TABLE_NAME='KEY_REQUEST';
TABLE_NAME    INDEX_NAME                            COLUMN_NAME  INDEX_TYPE_NAME  
KEY_REQUEST   PRIMARY_KEY_F                         REQUEST_ID   PRIMARY KEY
KEY_REQUEST   FK_53SHRBC21C25INSKPP1YOXXSS_INDEX_F  USER_ID      INDEX

如果 hibernate 看起来真的是在实际创建数据库之前尝试删除这些约束(即 hibernate 中的某种错误)。 有什么办法可以避免这些错误堵塞日志或表明某处真正的失败?

更新 1

试图强制应用程序仅使用此设置进行更新:

spring.jpa.hibernate.ddl-auto=update

导致以下错误(所有其他错误消失):

org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000388: Unsuccessful: alter table lti_result add constraint FK_1cnh9amy5br8owkmafsrth3as foreign key (result_id) references lti_link
org.hibernate.tool.hbm2ddl.SchemaUpdate  : Constraint "FK_1CNH9AMY5BR8OWKMAFSRTH3AS" already exists; SQL statement: 
    alter table lti_result add constraint FK_1cnh9amy5br8owkmafsrth3as foreign key (result_id) references lti_link [90045-178]

注意:来源在这里:https://github.com/azeckoski/lti_starter

具体配置: https://github.com/azeckoski/lti_starter/blob/master/src/main/resources/application.properties

和型号: https://github.com/azeckoski/lti_starter/tree/master/src/main/java/ltistarter/model

【问题讨论】:

  • 看起来这可能是休眠中的一个错误。这是这里的普遍共识吗?
  • 我有同样的问题(在稍旧的版本上,4.2.8)。这些错误是良性的,Hibernate 会捕获这些异常并创建模式。但它确实看起来像一个错误。当您第一次创建模式时,放弃约束是没有意义的。 “创建”被实现为“替换”。

标签: java spring hibernate jpa h2


【解决方案1】:

试试create-only:

spring.jpa.properties.hibernate.hbm2ddl.auto=create-only

它至少从 5.2 开始(可能更早)被添加到 Hibernate 中,我没有找到任何合适的文档,但它提到了 User Guide: 23.15. Automatic schema generation

如果您在日志中看到:

WARN  SessionFactoryOptionsBuilder:394 - Unrecognized hbm2ddl_auto value : create-only.  Supported values include 'create', 'create-drop', 'update', 'none' and 'validate'.  Ignoring

这可能是个误报,in fact it works

【讨论】:

  • 这个对我有用,但我很好奇它是否会对架构产生任何影响?我有“更新”作为价值,我有各种烦人的警告。一旦我将其更改为“仅创建”,它们都消失了。但是从“更新”到“仅创建”可以吗?
  • 嗯...对于内存中的 H2 这并不重要,因为数据库将在退出时被销毁。
  • 对于任何其他持久性数据库,您必须使用 Liqubase/Flyweight/DBMaintain 或任何其他迁移工具
【解决方案2】:

谢谢,这对我有用。

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create
logging.level.org.hibernate.SQL= DEBUG
hibernate.hbm2ddl.auto=update

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# Enable response compression
server.compression.enabled=true
# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024
server.http2.enabled=true
# Maximum time the response should be cached (in seconds)
spring.resources.cache.cachecontrol.max-age=120
# The cache must re-validate stale resources with the server. Any expired resources must not be used without re-validating.
spring.resources.cache.cachecontrol.must-revalidate=true
#https://www.callicoder.com/configuring-spring-boot-application/
server.port=8080

【讨论】:

  • 抱歉,您的示例与我们试图解决的问题无关
【解决方案3】:

由于您使用的是内存数据库,Hibernate 在执行时不会找到任何表:

hibernate.hbm2ddl.auto=create-drop

那是因为语句顺序是:

  • 删除约束 (fk)
  • 删除表格
  • 创建表
  • 创建约束(fk)

    Query:{[alter table tableIdentifier drop constraint FK_202gbutq8qbxk0chvcpjsv6vn][]} 
    ERROR [main]: o.h.t.h.SchemaExport - HHH000389: Unsuccessful: alter table tableIdentifier drop constraint FK_202gbutq8qbxk0chvcpjsv6vn
    ERROR [main]: o.h.t.h.SchemaExport - user lacks privilege or object not found: PUBLIC.TABLEIDENTIFIER
    Query:{[drop table sequenceIdentifier if exists][]} 
    Query:{[drop table tableIdentifier if exists][]} 
    Query:{[create table sequenceIdentifier (id bigint not null, primary key (id))][]} 
    Query:{[create table tableIdentifier (id bigint not null, sequenceIdentifier_id bigint, primary key (id))][]} 
    Query:{[alter table tableIdentifier add constraint FK_202gbutq8qbxk0chvcpjsv6vn foreign key (sequenceIdentifier_id) references sequenceIdentifier][]} 
    Query:{[create sequence hibernate_sequence start with 1 increment by 1][]} 
    

您可以通过将 hibernate.hbm2ddl.auto 更改为更新来解决此问题:

hibernate.hbm2ddl.auto=update

【讨论】:

  • 这非常接近,但会导致以下错误:2014-07-14 org.hibernate.tool.hbm2ddl.SchemaUpdate:HHH000388:不成功:更改表 lti_result 添加约束 FK_1cnh9amy5br8owkmafsrth3as 外键 (result_id) 引用 lti_link org.hibernate.tool.hbm2ddl.SchemaUpdate:约束“FK_1CNH9AMY5BR8OWKMAFSRTH3AS”已经存在; SQL 语句:alter table lti_result 添加约束 FK_1cnh9amy5br8owkmafsrth3as 外键 (result_id) 引用 lti_link [90045-178] 我将用额外的细节更新问题
  • 这就是我使用 Flyway 增量更新而不是依赖 Hibernate 模式生成的原因。
  • 那么你是说如果我使用hibernate就没有办法避免这些故障吗?
  • 一个常见的误解是 create-drop 先删除表,然后再创建。事实上,正如 create-drop 中的术语顺序所建议的那样,它首先创建表,然后在会话工厂被销毁时删除它们。请参阅 Hibernate 文档:docs.jboss.org/hibernate/orm/3.3/reference/en/html/…。所以这不能解释 azeckoski 的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2019-01-12
  • 1970-01-01
  • 2016-05-15
  • 2019-09-05
  • 2017-05-28
  • 1970-01-01
相关资源
最近更新 更多