Quarkus Hibernate ORM - import.sql not executed



为了测试目的,我使用Quarkus Devservices来设置postgres db。

如中所述https://quarkus.io/blog/hibernate-orm-config-profiles/我将generation设置为drop-and-create,并创建了import.sql文件,每次启动应用程序时都必须使用该文件从头开始创建数据库表,然而,没有创建任何内容,相反,我收到了不符合postgres数据库状态的奇怪错误消息。

2022-03-12 13:43:29,851 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-03-12 13:43:29,852 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) relation "filepart" does not exist, skipping
2022-03-12 13:43:29,853 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-03-12 13:43:29,854 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) table "download" does not exist, skipping
2022-03-12 13:43:29,855 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-03-12 13:43:29,855 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) table "filepart" does not exist, skipping
2022-03-12 13:43:29,867 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 42P07
2022-03-12 13:43:29,867 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) relation "download" already exists, skipping
2022-03-12 13:43:29,868 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 42P07
2022-03-12 13:43:29,868 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) relation "filepart" already exists, skipping
2022-03-12 13:43:51,035 INFO  [io.quarkus] (Quarkus Main Thread) quarkus-resteasy-postgres 1.0.0-SNAPSHOT on JVM (powered by Quarkus 2.7.4.Final) started in 27.125s. Listening on: http://localhost:8080

application.properties:

quarkus.datasource.devservices.enabled=true
quarkus.datasource.db-kind=postgresql
quarkus.datasource.devservices.port=5432
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres
quarkus.hibernate-orm.database.generation=drop-and-create
# it is default, only explicit to be clear:
quarkus.hibernate-orm.sql-load-script=import.sql

完整的项目在这里:https://github.com/syr/quarkus-resteasy-postgres/tree/hibernate_drop_not_working

UPDATE:禁用生成数据库时

quarkus.hibernate-orm.database.generation=none

显然是由实体生成的SQL仍在执行,而import.SQL则被忽略。

create table Download (idProperty1 varchar(255) not null, idProperty2 varchar(255) not null, finished boolean, id int8, primary key (idProperty1, idProperty2));
create table FilePart (id int8 not null, filePartFilePath varchar(255), idProperty1 varchar(255), idProperty2 varchar(255), primary key (id));
alter table if exists FilePart add constraint FKia6okjgd7yxo21sfga0hej3ni foreign key (idProperty1, idProperty2) references Download; 

请参阅https://quarkus.io/guides/hibernate-orm#quarkus-hibernate-orm_quarkus.hibernate-orm.scripts-database-scripts-related配置

财产";hibernate orm.sql加载脚本"用于加载数据(主要是插入语句(。

对于DDL(创建和删除表(,您可能必须使用以下属性:

quarkus.hibernate-orm.scripts.generation=drop-and-create
quarkus.hibernate-orm.scripts.generation.create-target=create.sql
quarkus.hibernate-orm.scripts.generation.drop-target=drop.sq

当模式发生变化(如创建新表(时,会调用import.sql。因此,只有当您将数据库生成更改为删除和创建时,它才会运行。

quarkus.hibernate-orm.scripts.generation=drop-and-create

数据库初始化后,您可以将数据库生成设置更改回更新

quarkus.hibernate-orm.scripts.generation=update

最新更新