Hibernate H2指定删除表的顺序 [英] Hibernate H2 specify drop table order
问题描述
我有几乎相同的问题,例如此用户.在每个SpringBootTest之后(例如运行mvn测试时),Hibernate不能删除我的内存中测试数据库的表.所需的行为是ddl-auto=create-drop
,但这不起作用.
I have pretty much the same problem like this user. Hibernate can't drop the tables of my in-memory test database after each SpringBootTest (e.g. when running mvn test). The desired behavior would be ddl-auto=create-drop
, but this doesn't work.
我认为原因可能是DROP TABLE
语句的无效顺序,因此Hibernate尝试删除其他表仍依赖的表.
I think the reason could be an invalid order of the DROP TABLE
statements, so that Hibernate tries to delete tables on which other tables still depend on.
我的data.sql脚本仅包含INSERT
语句,并且该架构是根据我的实体自动创建的.我尝试将DROP TABLE
语句添加到data.sql的顶部,它们全部通过(ddl-auto=create
),因为我可以指定必须删除它们的顺序.
另一方面,我现在也必须在data.sql中指定架构创建..
My data.sql script only contains INSERT
statements and the schema is automatically created based on my entities. I tried adding DROP TABLE
statements to the top of data.sql and they all pass (ddl-auto=create
), because I can specify the order in which they have to be dropped.
On the other side, I now have to specify the schema creation in data.sql too..
是否有一种方法可以指定drop语句的顺序,而不必指定架构的创建?还是有人知道最初的问题的解决方案?
Is there a way to specify the order of drop statements while not having to specify the schema creation? Or does anyone know a solution for the initial problem?
我想举一个例子.我有一个User
实体,该实体与其他实体(M:N,1:N,1:1)有关系.创建架构后,hibernate会删除所有表,创建它们并添加约束:
I want to give an example. I have an User
entity that has relationships to other entities (M:N, 1:N, 1:1). When the schema is created, hibernate drops all tables, creates them and adds constrains:
// first test file:
Hibernate: drop table user if exists
... // drop other tables
Hibernate: create table user (username varchar(255) not null, ... , primary key (username))
... // create other tables
Hibernate: alter table X add constraint FKgi38hy0tsrdm332gdjrc0uhm3 foreign key (username) references user
Hibernate: alter table Y add constraint FK5svpy1b71l4jxni0xylrbbdtv foreign key (username) references user
Hibernate: alter table Z add constraint FK5a8fxbb0ug3eo1lisdrrxbbj foreign key (username) references user
// next test file:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Cannot drop "USER" because "FKGI38HY0TSRDM332GDJRC0UHM3, FK5SVPY1B71L4JXNI0XYLRBBDTV, FK5A8FXBB0UG3EO1LISDRRXBBJ" depends on it; SQL statement:
drop table user if exists [90107-200]
此过程在第一个测试文件之后不起作用,因为它违反了约束.这就是为什么我要指定放置顺序的原因.
This process doesn't work after the first test file, because it violates the constrains. This is why I wanted to specify the drop order.
我不在实体上使用CascadeType,这可能会导致问题吗?
I don't use CascadeType on my entities, could that cause the problem?
推荐答案
我终于找到了解决我的问题的方法.
就像我说的那样,错误是由于尝试删除仍依赖于其他表的表而引起的.我以为这可能与缺少CascadeType
规范有关,但我无法解决.
I finally found a work around for my problem.
As I said, the errors where caused by trying to drop tables on which other tables still depend on. I thought that this could be related to missing CascadeType
specifcations, but I couldn't fix it.
然后我找到了此答案,它对我有用.除了我的data.sql
文件(所有数据都插入到自动创建的架构中)之外,我现在还有一个drop-tables.sql
,在其中可以指定DROP
语句的正确顺序.此文件在自动创建模式之前执行,因此解决了我的问题.
Then I found this answer and it works for me. Additional to my data.sql
file (where all my data is inserted to the auto-created schema) I now have a drop-tables.sql
where I can specify the correct order of DROP
statements. This file is executed before the automatic schema creation and therefore solves my problem.
application.properties:
spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create
spring.jpa.properties.javax.persistence.schema-generation.drop-source=script-then-metadata
spring.jpa.properties.javax.persistence.schema-generation.drop-script-source=drop-tables.sql
这篇关于Hibernate H2指定删除表的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!