How to see the schema sql (DDL) in spring boot?

谁说胖子不能爱 提交于 2020-12-30 07:42:24

问题


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.

  • Adding the following to application.properties file
    • debug=true
    • spring.jpa.properties.hibernate.show_sql=true
  • Set org.hibernate.SQL level to debug in logback.xml
  • Steps listed at http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

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

回答1:


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/



来源:https://stackoverflow.com/questions/37648395/how-to-see-the-schema-sql-ddl-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!