1

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?

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101
  • 2
    No because both are different properties. Set the `spring.jpa.hibernate.ddl-auto` property to `none` then it should take precedence. – M. Deinum Apr 08 '19 at 18:32
  • Out of curiosity, why would you *not* want the schema to be generated for an in-memory database? – crizzis Apr 08 '19 at 20:22
  • @crizzis Well I was having errors with the auto generation so I needed to turn it off while I figured it out and worked on other things – secondbreakfast Apr 09 '19 at 14:10
  • @crizzis a scenario is when already exists either the `schema.sql` or `schema-h2.sql` files. Therefore the schema already exists. – Manuel Jordan Jul 10 '23 at 15:15
  • @M.Deinum even when `spring.jpa.hibernate.ddl-auto` as **none** works as expected - Why is available the `spring.jpa.generate-ddl` property if the `spring.jpa.hibernate.ddl-auto` property handles the same approach to reach the same goal?- is not clear when is mandatory use `spring.jpa.generate-ddl` over `spring.jpa.hibernate.ddl-auto` – Manuel Jordan Jul 10 '23 at 15:18
  • 1
    `generate-ddl` is only `true` and `false` which works with any provider. Whereas `spring.jpa.hibernate.ddl-auto` allows you to specify the specify hibernate mode. – M. Deinum Jul 10 '23 at 18:03
  • @ManuelJordan you do realize that the question asked was about an **in-memory** database? – crizzis Jul 11 '23 at 14:41
  • @crizzis - Yes, but it could be applied to in a disk database as MySQL for _pre-production_ profile ... it to test with the target DB – Manuel Jordan Jul 11 '23 at 15:45
  • @M.Deinum thanks for the reply, yes the former is generic and the latter has more options. – Manuel Jordan Jul 11 '23 at 15:46

1 Answers1

3

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

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Dominik
  • 1,058
  • 8
  • 20