java - 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)
解决方案
- 从这个链接
- 默认情况下,仅当您使用嵌入式数据库(H2、HSQL 或 Derby)时,才会自动创建 JPA 数据库。
您可以使用spring.jpa.*
属性显式配置 JPA 设置。例如,要创建和删除表,您可以将以下行添加到 application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
Hibernate 自己的内部属性名称(如果你碰巧记得更好的话)是hibernate.hbm2ddl.auto
.
- 从这个链接
spring.jpa.hibernate.ddl-auto
这实际上是"hibernate.hbm2ddl.auto"
属性的快捷方式。
默认为"create-drop"
使用嵌入式数据库且未检测到模式管理器时。否则,默认为"none"
.
- 从这个链接
Spring Boot 可以自动创建 DataSource 的模式(DDL 脚本)并对其进行初始化(DML 脚本)。
它分别从标准根类路径位置加载 SQL:
schema.sql
和data.sql
。此外,Spring Boot 处理
schema-${platform}.sql
和data-${platform}.sql
文件(如果存在),其中平台是spring.datasource.platform
.这允许您在必要时切换到特定于数据库的脚本。例如,您可以选择将其设置为数据库的供应商名称(hsqldb、h2、oracle、mysql、postgresql 等)。
推荐阅读
- jmeter - 分布式模式下的 Jmeter:JVM 不会因为 BeanShell 服务器而停止
- statistics - H2O 中的平均残差公式
- python - Can't find Element with Selenium
- java - 回到主要活动
- javascript - Webpack stop working after switch to different branch then switch back
- c++ - 以递归方式从文件中提升 C++ 序列化和反序列化对象
- arrays - Finding the first and last rows that contain a black pixel, in a numpy array
- node.js - Azure Function automatic retry on failure UnhandledPromiseRejectionWarning
- php - Wordpress:分层标签端点的问题
- php - Symfony 4 在控制器中设置 Twig 模板路径