spring.jpa.hibernate.ddl-auto属性在Spring中如何正常工作? [英] How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?
问题描述
我当时正在从事Spring Boot应用程序项目,并注意到,有时到另一台服务器(SQL Server)上的数据库存在连接超时错误.
当我尝试使用FlyWay
进行一些脚本迁移时,会发生这种情况,但是经过几次尝试后,它仍然可以工作.
I was working on my Spring boot app project and noticed that, sometimes there is a connection time out error to my Database on another server(SQL Server).
This happens specially when I try to do some script migration with FlyWay
but it works after several tries.
然后,我注意到我没有在属性文件中指定spring.jpa.hibernate.ddl-auto
.我做了一些研究,发现建议添加
spring.jpa.hibernate.ddl-auto= create-drop
正在开发中.
并将其更改为:生产中的spring.jpa.hibernate.ddl-auto= none
.
Then I noticed that I didn't specify spring.jpa.hibernate.ddl-auto
in my properties file. I did some research and found that it is recommended to add
spring.jpa.hibernate.ddl-auto= create-drop
in development.
And change it to: spring.jpa.hibernate.ddl-auto= none
in production.
但是我实际上并没有真正理解它是如何工作的,以及休眠如何使用create-drop
或none
值生成数据库架构的.您能否从技术上解释它是如何工作的,以及在开发和生产服务器上使用此属性的建议?
谢谢
But I didn't actually understand how does it really work and how does hibernate generate database schema using create-drop
or none
value. Can you please explain technically how does it really work, and what are recommendations for using this property in development and on a production server.
Thank you
推荐答案
对于记录,spring.jpa.hibernate.ddl-auto
属性是特定于Spring Data JPA的,并且是他们指定最终将在其属性下传递给Hibernate的值的方式.知道,hibernate.hbm2ddl.auto
.
For the record, the spring.jpa.hibernate.ddl-auto
property is Spring Data JPA specific and is their way to specify a value that will eventually be passed to Hibernate under the property it knows, hibernate.hbm2ddl.auto
.
值create
,create-drop
,validate
和update
基本上影响架构工具管理在启动时如何操作数据库架构.
The values create
, create-drop
, validate
, and update
basically influence how the schema tool management will manipulate the database schema at startup.
例如,update
操作将查询JDBC驱动程序的API以获取数据库元数据,然后Hibernate根据读取的带注释的类或HBM XML映射比较它创建的对象模型,并尝试在以下位置调整架构:飞行中.
For example, the update
operation will query the JDBC driver's API to get the database metadata and then Hibernate compares the object model it creates based on reading your annotated classes or HBM XML mappings and will attempt to adjust the schema on-the-fly.
例如,update
操作将尝试添加新的列,约束等,但绝不会删除以前可能存在但不再作为先前运行的对象模型一部分的列或约束.
The update
operation for example will attempt to add new columns, constraints, etc but will never remove a column or constraint that may have existed previously but no longer does as part of the object model from a prior run.
通常在测试用例场景中,您可能会使用create-drop
,以便创建架构,测试用例添加一些模拟数据,运行测试,然后在测试用例清理期间删除架构对象,留下一个空的数据库.
Typically in test case scenarios, you'll likely use create-drop
so that you create your schema, your test case adds some mock data, you run your tests, and then during the test case cleanup, the schema objects are dropped, leaving an empty database.
在开发中,通常会看到开发人员使用update
自动修改架构以在重新启动时添加新的内容.但是要再次理解,这不会删除以前执行中可能不再存在的列或约束.
In development, it's often common to see developers use update
to automatically modify the schema to add new additions upon restart. But again understand, this does not remove a column or constraint that may exist from previous executions that is no longer necessary.
在生产中,通常强烈建议您使用none
或根本不指定此属性.这是因为DBA通常会检查迁移脚本以了解数据库更改,特别是如果您的数据库在多个服务和应用程序之间共享时.
In production, it's often highly recommended you use none
or simply don't specify this property. That is because it's common practice for DBAs to review migration scripts for database changes, particularly if your database is shared across multiple services and applications.
这篇关于spring.jpa.hibernate.ddl-auto属性在Spring中如何正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!