Hibernate H2 specify drop table order

后端 未结 2 1259
天涯浪人
天涯浪人 2021-01-06 02:42

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). Th

相关标签:
2条回答
  • 2021-01-06 03:38

    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.

    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
    
    0 讨论(0)
  • 2021-01-06 03:48

    You must annotate your test classes with DirtiesContext:

    @DirtiesContext(methodMode = MethodMode.BEFORE_CLASS)
    

    This will rebuild the test context and therefore also create-drop your schema.

    Read more about this: https://www.baeldung.com/spring-dirtiescontext

    0 讨论(0)
提交回复
热议问题