spring.jpa.hibernate.hbm2ddl和spring.jpa.hibernate.ddl之间的区别 [英] Difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl
问题描述
spring.jpa.hibernate.hbm2ddl
和 spring.jpa.hibernate.ddl
有什么区别?
What is the difference between spring.jpa.hibernate.hbm2ddl
and spring.jpa.hibernate.ddl
?
我在以下问题中发现:什么是OP在属性中同时使用了spring.datasource.initialization-mode?a>的可能值,但是hbm2ddl
的来源似乎直接休眠而不是Spring Data Jpa.
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.
尽管如此,从另一个OP中读取答案,它看起来像只是传递.
Nevertheless, reading the answer from another OP, it looks like pass-through only.
但是,在我们使用mariadb的商业项目中,当我们没有通过 spring.jpa.hibernate.hbm2ddl.auto=create
正常关闭Spring Boot应用程序时,再次运行该应用程序时,它将删除旧数据并从头开始创建所有内容.另一方面,使用 spring.jpa.hibernate.ddl.auto=create
每秒运行一次(在没有正常关闭应用程序之后)会导致关键约束异常(数据库在创建之前没有被删除)
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:You can explicitly configure JPA settings by using
spring.jpa.*
properties. For example, to create and drop tables you can add the following line to your application.properties:spring.jpa.hibernate.ddl-auto=create-drop
为此,Hibernate自己的内部属性名称是
hibernate.hbm2ddl.auto
(如果您还记得的话).Hibernate’s own internal property name for this (if you happen to remember it better) is
hibernate.hbm2ddl.auto
.- 从此链接
spring.jpa.hibernate.ddl-auto
这实际上是"hibernate.hbm2ddl.auto"
属性的快捷方式.spring.jpa.hibernate.ddl-auto
This is actually a shortcut for the"hibernate.hbm2ddl.auto"
property.使用嵌入式数据库且未检测到任何模式管理器时,默认为
"create-drop"
.否则,默认为"none"
.Defaults to
"create-drop"
when using an embedded database and no schema manager was detected. Otherwise, defaults to"none"
.