Search code examples
javaspringhibernatespring-bootjpa

H2 schema still generated when spring.jpa.generate-ddl=false


I am using Spring Boot 2.1.3 with an H2 in memory database for testing. When I run my tests, the schema gets generated even when I specify the following property.

spring.jpa.generate-ddl=false

It seems that because Spring Boot defaults the following property when using H2

spring.jpa.hibernate.ddl-auto=create-drop

That this takes precedence over spring.jpa.generate-ddl=false

Is this a bug?


Solution

  • This behavior is described in the Spring Boot Reference Documentation at the Creating and Dropping JPA Databases section as follows:

    By default, the DDL execution (or validation) is deferred until the ApplicationContext has started. There is also a spring.jpa.generate-ddl flag, but it is not used if Hibernate auto-configuration is active, because the ddl-auto settings are more fine-grained.

    Since the spring.jpa.hibernate.ddl-auto property is set by default when Hibernate is used, therefore the spring.jpa.generate-ddl property is ignored, at least with a H2 in-memory database