4

For a Spring Boot app (2.4.1) working with Spring Data JPA, with @Entity classes based with the javax.persistence annotations. For a application.properties exists these two properties:

  • spring.jpa.generate-ddl
  • spring.jpa.hibernate.ddl-auto

Thanks to STS, the following descriptions are available:

spring.jpa.generate-ddl

spring.jpa.generate-ddl
java.lang.Boolean

Default: false

Whether to initialize the schema on startup.

spring.jpa.hibernate.ddl-auto

spring.jpa.hibernate.ddl-auto
java.lang.String

DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. 
Defaults to "create-drop" when using an embedded database and no schema manager was detected. 
Otherwise, defaults to "none"
  • When is used the spring.jpa.generate-ddl?
  • When is mandatory use the spring.jpa.generate-ddl approach? What scenario?

I did realise that it is ignored and really is mandatory use spring.jpa.hibernate.ddl-auto, it for the following scenario:

  • For the H2 in-memory database - the schema is created through the schema.sql file and defining the spring.jpa.hibernate.ddl-auto property as none, then in this case spring.jpa.generate-ddl is totally ignored.

Question:

  • What is the relation (if exists) and differences between spring.jpa.generate-ddl and spring.jpa.hibernate.ddl-auto?

I found these posts, and in someway is not clear when is mandatory use spring.jpa.generate-ddl

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

1 Answers1

2

In this Spring Boot Database Initialization article you can find the differences well explained.

Just to summarize they are pretty much used for the same goal. The goal is to define the behaviour of your application after any modification on your entity classes. Given that, The first one permits you generic choices instead the second one permits you to decide within a pool of behaviours.

(I would define just one of them also to avoid conflicts.)

So:

  1. spring.jpa.generate-ddl – This configuration takes a boolean value to either enable or disable schema initialization.

So imagine that you are adding/removing a field in the entity User: In this case:

  • true: it will change your schema automatically when you run the app
  • false: no changes on the schema. App will fail if some call are made to the unsync entity/db.
  1. spring.jpa.hibernate.ddl-auto – This property takes an enum that controls the schema generation in a more controlled way.

Here is the interesting part, there are more scenarios based on your choice:

  • none : same behaviour of "false" of generate-ddl
  • create : drop the last schema of and create a new one. It drops the data too so be sure not to use that in prod.
  • create-drop : same of the "create" option but the scheam is destroyed at the shutdown and not at the next run of your application.
  • validate : the app check the Entities element and the DB. If there are inconsistencies then gives you back some Exception.
  • update : here, in case of a new column for example, the application doesn’t create the entire table. It will adds the new column and creates a unique index.

Everything is explained (better) in the article with a proper image to show you! Enjoy!

P.S.

If you are trying to find the difference also between spring.jpa.hibernate.ddl-auto and hibernate.hbm2ddl.auto please check this other link

spring.jpa.hibernate.ddl-auto This is actually a shortcut for the "hibernate.hbm2ddl.auto" property.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158