spring.jpa.hibernate.ddl-auto 属性在 Spring 中是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42135114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:20:39  来源:igfitidea点击:

How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?

springhibernatejpadatabase-connectionddl

提问by METTAIBI

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 FlyWaybut it works after several tries.

我正在处理我的 Spring Boot 应用程序项目并注意到,有时我的另一台服务器(SQL Server)上的数据库会出现连接超时错误。当我尝试进行一些脚本迁移时会特别发生这种情况,FlyWay但经过多次尝试后它才起作用。

Then I noticed that I didn't specify spring.jpa.hibernate.ddl-autoin my properties file. I did some research and found that it is recommended to add spring.jpa.hibernate.ddl-auto= create-dropin development. And change it to: spring.jpa.hibernate.ddl-auto= nonein production.

然后我注意到我没有spring.jpa.hibernate.ddl-auto在我的属性文件中指定 。我做了一些研究,发现建议spring.jpa.hibernate.ddl-auto= create-drop在开发中添加 。并将其更改为:spring.jpa.hibernate.ddl-auto= none在生产中。

But I didn't actually understand how does it really work and how does hibernate generate database schema using create-dropor nonevalue. 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

但我实际上并不了解它是如何真正工作的,以及 hibernate 如何使用create-dropnone值生成数据库模式。您能否从技术上解释它是如何真正工作的,以及在开发和生产服务器上使用此属性的建议是什么。谢谢

回答by Naros

For the record, the spring.jpa.hibernate.ddl-autoproperty 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.

作为记录,该spring.jpa.hibernate.ddl-auto属性是 Spring Data JPA 特定的,并且是他们指定最终将在它知道的属性下传递给 Hibernate 的值的方式hibernate.hbm2ddl.auto

The values create, create-drop, validate, and updatebasically influence how the schema tool management will manipulate the database schema at startup.

createcreate-dropvalidateupdate基本上影响模式工具管理在启动时操作数据库模式的方式。

For example, the updateoperation 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操作将查询 JDBC 驱动程序的 API 以获取数据库元数据,然后 Hibernate 根据读取您的带注释的类或 HBM XML 映射来比较它创建的对象模型,并尝试即时调整模式。

The updateoperation 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.

update例如,该操作将尝试添加新列、约束等,但永远不会删除以前可能存在但不再作为先前运行的对象模型的一部分的列或约束。

Typically in test case scenarios, you'll likely use create-dropso 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.

通常在测试用例场景中,您可能会使用create-drop这样的方法来创建架构,测试用例添加一些模拟数据,运行测试,然后在测试用例清理期间,删除架构对象,留下一个空数据库。

In development, it's often common to see developers use updateto 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.

在开发中,经常会看到开发人员使用update自动修改架构以在重新启动时添加新内容。但是再次理解,这不会删除以前执行中可能存在的不再需要的列或约束。

In production, it's often highly recommended you use noneor 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.

在生产中,通常强烈建议您使用none或干脆不指定此属性。这是因为 DBA 检查数据库更改的迁移脚本是一种常见做法,尤其是当您的数据库在多个服务和应用程序之间共享时。