spring.jpa.hibernate.ddl-auto属性在Spring中如何工作? [英] how does exactly spring.jpa.hibernate.ddl-auto property works in Spring?

查看:2805
本文介绍了spring.jpa.hibernate.ddl-auto属性在Spring中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在开发Spring引导应用程序项目,并且我注意到有时连接超时到另一台服务器上的数据库(SQL Server),特别是当我尝试使用FlyWay执行一些脚本迁移时,但它在几次尝试后工作。然后我注意到,我没有在我的属性spring.jpa.hibernate.ddl-auto中指定。然后我做了一些研究,发现在开发中推荐添加
spring.jpa.hibernate.ddl-auto = create-drop
并在生产中将其更改为: spring.jpa.hibernate.ddl-auto = none 。但是我没有真正理解它是如何工作的,以及hibernate如何使用create value或none来生成数据库模式。您可以请技术上解释它是如何工作的,以及在开发和生产服务器中使用此属性的建议是什么。
谢谢

解决方案

对于记录, spring.jpa.hibernate.ddl- auto 属性是Spring Data JPA特有的,并且是它们指定最终将传递给Hibernate的值的方法,该属性在它知道的属性 hibernate.hbm2ddl.auto $ b

create create-drop validate update 基本上影响架构工具管理在启动时如何操作数据库模式。例如, update 操作将查询JDBC驱动程序的API以获取数据库元数据,然后Hibernate将对象模型它创建的基础是读取您的注释类或HBM XML映射,并将尝试即时调整模式。
$ b update 例如code>操作将尝试添加新的列,约束等,但不会删除可能拥有的列或约束ve已经存在,但不再作为之前运行的对象模型的一部分。



通常在测试用例场景中,您可能会使用 create-drop ,以便创建模式,测试用例添加一些模拟数据,运行测试,然后在测试用例清理期间,模式对象被删除,留下一个空的数据库。 / p>

在开发过程中,通常会发现开发人员使用 update 自动修改模式以在重新启动时添加新增内容。但是,再次理解,这并不会删除以前执行时可能存在的不再需要的列或约束。



在生产中,通常强烈建议您使用 none 或者根本不指定此属性。这是因为数据库管理员通常会检查数据库更改的迁移脚本,特别是如果您的数据库跨多个服务和应用程序共享。


actually i was working on my Spring boot app project, and i noticed that sometimes there is a connection time out to my Database on another server(SQL Server), especially when i try to do some script migration with FlyWay, but it works after several tries. I noticed then that i didn't specify in my properties spring.jpa.hibernate.ddl-auto. I then did some research and found that it is recommanded to add spring.jpa.hibernate.ddl-auto= create-drop in developement. And change it to: spring.jpa.hibernate.ddl-auto= none in production. But i didn't actually understood how does it really work, and how does hibernate generate database schema, with create value or using none. Can you please explain technically how does it really work, and what are recommandations using this property in developpment and production server. Thank you

解决方案

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.

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

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.

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.

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.

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆