首页 > 解决方案 > Difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl

问题描述

What is the difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl?

I have found in this question: What are the possible values of spring.datasource.initialization-mode? that OP is using both in properties, however it seems like the origin of hbm2ddl is hibernate directly not Spring Data Jpa.

Nevertheless, reading the answer from another OP, it looks like pass-through only.

However in our commercial project with mariadb, when we do not close our spring boot application gracefully with spring.jpa.hibernate.hbm2ddl.auto=create, when the application is run again, it deletes old data and creates everything from scratch. On the other hand with spring.jpa.hibernate.ddl.auto=create every second run (after no graceful application shutdown) causes key constraint exceptions (DB is not being dropper before creation)

标签: javaspringhibernatespring-bootjpa

解决方案


  1. 从这个链接
  • 默认情况下,仅当您使用嵌入式数据库(H2、HSQL 或 Derby)时,才会自动创建 JPA 数据库。

您可以使用spring.jpa.*属性显式配置 JPA 设置。例如,要创建和删除表,您可以将以下行添加到 application.properties:

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

Hibernate 自己的内部属性名称(如果你碰巧记得更好的话)是hibernate.hbm2ddl.auto.

  1. 从这个链接

spring.jpa.hibernate.ddl-auto这实际上是"hibernate.hbm2ddl.auto"属性的快捷方式。

默认为"create-drop"使用嵌入式数据库且未检测到模式管理器时。否则,默认为"none".

  1. 从这个链接
  • Spring Boot 可以自动创建 DataSource 的模式(DDL 脚本)并对其进行初始化(DML 脚本)。

  • 它分别从标准根类路径位置加载 SQL:schema.sqldata.sql

  • 此外,Spring Boot 处理schema-${platform}.sqldata-${platform}.sql文件(如果存在),其中平台是spring.datasource.platform.

  • 这允许您在必要时切换到特定于数据库的脚本。例如,您可以选择将其设置为数据库的供应商名称(hsqldb、h2、oracle、mysql、postgresql 等)。


推荐阅读