且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

启动H2数据库时,Hibernate生成Drop Constraint错误的Spring Boot

更新时间:2022-10-20 09:03:31

因为您使用的是内存数据库,所以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