8

How can I see the DDL SQL generated by Hibernate for building the schema from the JPA mappings? I am using the embedded HSQL db.

I tried the following and none of them worked in Spring-Boot 1.3.5.RELEASE.

Those only show me the sql issued by Hibernate for queries. I am looking for the DDL schema sql issued by Hibernate due the following property:

spring.jpa.hibernate.ddl-auto=create-drop
Suneel
  • 817
  • 3
  • 10
  • 23
  • Have you seen [this](http://stackoverflow.com/questions/2536829/hibernate-show-real-sql) SO question? – Tim Biegeleisen Jun 06 '16 at 01:23
  • @TimBiegeleisen Yes, but they are the same as above. They only show the queries, not DDL issued by Hibernate when the app boots up. – Suneel Jun 06 '16 at 01:31
  • the show-sql only shows the DMLs, you will need to add `debug=true` toapplication.properties to see the DDLs – Shibashis Jun 06 '16 at 03:30
  • @Shibashis I tried `debug=true` as mentioned in my posting already. That still didn't show the DDLs. – Suneel Jun 06 '16 at 10:49

1 Answers1

14

Try with this property and value:

javax.persistence.schema-generation.scripts.action=create

Do not forget to also set this property:

javax.persistence.schema-generation.scripts.create-target=my-schema.sql

From the JPA 2.1 Specifiation, page 370:

javax.persistence.schema-generation.scripts.action

The javax.persistence.schema-generation.scripts.action property specifies which scripts are to be generated by the persistence provider. The values for this property are none, create, drop-and-create, drop. A script will only be generated if the script target is specified. If this property is not specified, it is assumed that script generation is not needed or will

In Spring Boot you can define those two properties in your application.properties file:

spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=build/my-schema.sql
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create

Here is a blog post about JPA schema generation with further information about these and other properties: http://www.thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/

Thomas Traude
  • 840
  • 7
  • 17
  • 3
    Hint: This disables the actual schema generation. If you want to create the schema.sql script AND want hibernate to create the schema in the actual DB you must add `spring.jpa.properties.javax.persistence.schema-generation.database.action=create` – Robert Jan 06 '19 at 11:26
  • Article which goes into more details about this: https://www.baeldung.com/spring-data-jpa-generate-db-schema – payne Nov 13 '20 at 20:20