Java Hibernate hbm2ddl.auto 配置的可能值是什么,它们有什么作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/438146/
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
What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
提问by
I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto
I need to know when to use the update and when not? And what is the alternative?
我真的很想知道更多关于更新、导出和可以给hibernate.hbm2ddl.auto
我的值我需要知道什么时候使用更新,什么时候不使用?什么是替代方案?
These are changes that could happen over DB:
这些是可能发生在 DB 上的更改:
- new tables
- new columns in old tables
- columns deleted
- data type of a column changed
- a type of a column changed its attributes
- tables dropped
- values of a column changed
- 新表
- 旧表中的新列
- 列已删除
- 列的数据类型已更改
- 一种类型的列改变了它的属性
- 桌子掉了
- 列的值已更改
In each case what is the best solution?
在每种情况下,最佳解决方案是什么?
回答by Peter Hilton
The configuration property is called hibernate.hbm2ddl.auto
配置属性被称为 hibernate.hbm2ddl.auto
In our development environment we set hibernate.hbm2ddl.auto=create-drop
to drop and create a clean database each time we deploy, so that our database is in a known state.
在我们的开发环境中,我们设置hibernate.hbm2ddl.auto=create-drop
每次部署时删除并创建一个干净的数据库,以便我们的数据库处于已知状态。
In theory, you can set hibernate.hbm2ddl.auto=update
to update your database with changes to your model, but I would not trust that on a production database. An earlier version of the documentation said that this was experimental, at least; I do not know the current status.
理论上,您可以设置hibernate.hbm2ddl.auto=update
通过更改模型来更新数据库,但我不相信在生产数据库上这样做。文档的早期版本说这是实验性的,至少;我不知道目前的状态。
Therefore, for our production database, do not set hibernate.hbm2ddl.auto
- the default is to make no database changes. Instead, we manually create an SQL DDL update script that applies changes from one version to the next.
因此,对于我们的生产数据库,不要设置hibernate.hbm2ddl.auto
- 默认是不进行数据库更改。相反,我们手动创建一个 SQL DDL 更新脚本,将更改从一个版本应用到下一个版本。
回答by Pat
I would use liquibasefor updating your db. hibernate's schema update feature is really only o.k. for a developer while they are developing new features. In a production situation, the db upgrade needs to be handled more carefully.
我会使用liquibase来更新你的数据库。hibernate 的模式更新功能真的只适合开发人员在开发新功能时使用。在生产情况下,需要更仔细地处理数据库升级。
回答by James McMahon
From the community documentation:
来自社区文档:
hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
e.g. validate | update | create | create-drop
hibernate.hbm2ddl.auto 在创建 SessionFactory 时自动验证或导出模式 DDL 到数据库。使用 create-drop,当 SessionFactory 显式关闭时,数据库模式将被删除。
例如验证| 更新 | 创建 | 创建删除
So the list of possible options are,
所以可能的选项列表是,
- validate: validate the schema, makes no changes to the database.
- update: update the schema.
- create: creates the schema, destroying previous data.
- create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.
- none: does nothing with the schema, makes no changes to the database
- validate:验证架构,不对数据库进行任何更改。
- 更新:更新架构。
- create:创建模式,销毁以前的数据。
- create-drop:在 SessionFactory 显式关闭时删除架构,通常是在应用程序停止时。
- none:对架构不做任何事情,不对数据库做任何更改
These options seem intended to be developers tools and not to facilitate any production level databases, you may want to have a look at the following question; Hibernate: hbm2ddl.auto=update in production?
这些选项似乎旨在成为开发人员工具,而不是为任何生产级数据库提供便利,您可能需要查看以下问题;休眠:hbm2ddl.auto=生产中的更新?
回答by Michiel Verkaik
There's also the undocumented value of "none" to disable it entirely.
还有一个未记录的值“none”可以完全禁用它。
回答by Vishal Sharma
I Think you should have to concentrate on the
我认为你应该专注于
SchemaExport Class
this Class Makes Your Configuration Dynamic So it allows you to choose whatever suites you best...
这个类使您的配置动态因此它允许您选择最适合您的任何套件...
Checkout [SchemaExport]
回答by Vlad Mihalcea
First, the possible values for the hbm2ddl
configuration property are the following ones:
首先,hbm2ddl
配置属性的可能值如下:
none
- No action is performed. The schema will not be generated.create-only
- The database schema will be generated.drop
- The database schema will be dropped and created afterwards.create
- The database schema will be dropped and created afterwards.create-drop
- The database schema will be dropped and created afterwards. Upon closing the theSessionFactory
, the database schema will be dropped.validate
- The database schema will be validated using the entity mappings.update
- The database schema will be updated by comparing the existing database schema with the entity mappings.
none
- 不执行任何操作。将不会生成架构。create-only
- 将生成数据库模式。drop
- 数据库模式将在之后删除和创建。create
- 数据库模式将在之后删除和创建。create-drop
- 数据库模式将在之后删除和创建。关闭后SessionFactory
,数据库模式将被删除。validate
- 将使用实体映射验证数据库模式。update
- 将通过将现有数据库架构与实体映射进行比较来更新数据库架构。
I dedicated a blog postfor the most common Hibernate DDL generation strategies:
我专门写了一篇关于最常见的Hibernate DDL 生成策略的博客文章:
- The
hibernate.hbm2ddl.auto="update"
is convenient but less flexible if you plan on adding functions or executing some custom scripts. - The most flexible approachis to use Flyway.
hibernate.hbm2ddl.auto="update"
如果您计划添加函数或执行一些自定义脚本,这很方便但不太灵活。- 在最灵活的方法是使用迁飞。
However, even if you use Flyway, you can still generate the initial migration script using hbm2ddl. In this article, you can see how you can combine the JPA Entity Model with jOOQ Table Model.
但是,即使您使用 Flyway,您仍然可以使用 hbm2ddl 生成初始迁移脚本。在本文中,您可以看到如何将 JPA 实体模型与 jOOQ 表模型相结合。
回答by Stefan Haberl
If you don't want to use Strings in your app and are looking for predefined constants have a look at org.hibernate.cfg.AvailableSettings
class included in the Hibernate JAR, where you'll find a constant for all possible settings. In your case for example:
如果您不想在您的应用程序中使用字符串并且正在寻找预定义的常量,请查看org.hibernate.cfg.AvailableSettings
包含在 Hibernate JAR 中的类,您将在其中找到所有可能设置的常量。例如,在您的情况下:
/**
* Auto export/update schema using hbm2ddl tool. Valid values are <tt>update</tt>,
* <tt>create</tt>, <tt>create-drop</tt> and <tt>validate</tt>.
*/
String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
回答by amit
Although it is quite an old post but as i did some research on the topic so thought of sharing it.
虽然这是一篇很老的帖子,但因为我对这个话题做了一些研究,所以想分享它。
hibernate.hbm2ddl.auto
hibernate.hbm2ddl.auto
As per the documentation it can have four valid values:
根据文档,它可以有四个有效值:
create | update | validate | create-drop
创建 | 更新 | 验证 | 创建删除
Following is the explanation of the behaviour shown by these value:
以下是对这些值显示的行为的解释:
- create:- create the schema, the data previously present (if there) in the schema is lost
- update:- update the schema with the given values.
- validate:- validate the schema. It makes no change in the DB.
- create-drop:- create the schema with destroying the data previously present(if there). It also drop the database schema when the SessionFactory is closed.
- create:- 创建架构,架构中先前存在的数据(如果存在)将丢失
- 更新:- 使用给定的值更新架构。
- 验证:- 验证架构。它不会改变数据库。
- create-drop:- 通过破坏先前存在的数据(如果存在)来创建模式。当 SessionFactory 关闭时,它还会删除数据库模式。
Following are the important points worth noting:
以下是值得注意的要点:
- In case of update, if schema is not present in the DB then the schema is created.
- In case of validate, if schema does not exists in DB, it is not created. Instead, it will throw an error:-
Table not found:<table name>
- In case of create-drop, schema is not dropped on closing the session. It drops only on closing the SessionFactory.
In case if i give any value to this property(say abc, instead of above four values discussed above) or it is just left blank. It shows following behaviour:
-If schema is not present in the DB:- It creates the schema
-If schema is present in the DB:- updatethe schema.
- 在update 的情况下,如果数据库中不存在架构,则创建架构。
- 在validate 的情况下,如果 DB 中不存在 schema,则不会创建它。相反,它会抛出一个错误:-
Table not found:<table name>
- 在create-drop 的情况下,关闭会话时不会删除架构。它仅在关闭 SessionFactory 时下降。
如果我给这个属性任何值(比如 abc,而不是上面讨论的四个值)或者它只是留空。它显示以下行为:
- 如果数据库中不存在模式:- 它创建模式
- 如果数据库中存在架构:-更新架构。
回答by Doc Davluz
Since 5.0, you can now find those values in a dedicated Enum
: org.hibernate.boot.SchemaAutoTooling
(enhanced with value NONE
since 5.2).
从 5.0 开始,您现在可以在专用的Enum
: 中找到这些值org.hibernate.boot.SchemaAutoTooling
(NONE
从 5.2 开始增强了值)。
Or even better, since 5.1, you can also use the org.hibernate.tool.schema.Action
Enum
which combines JPA 2 and "legacy" Hibernate DDL actions.
或者更好的是,从 5.1 开始,您还可以使用结合了 JPA 2 和“传统”Hibernate DDL 操作的 。org.hibernate.tool.schema.Action
Enum
But, you cannot yet configure a DataSource
programmatically with this. It would be nicer to use this combined with org.hibernate.cfg.AvailableSettings#HBM2DDL_AUTO
but the current code expect a String
value (excerpt taken from SessionFactoryBuilderImpl
):
但是,您还不能使用它以DataSource
编程方式配置。结合使用它会更好,org.hibernate.cfg.AvailableSettings#HBM2DDL_AUTO
但当前代码需要一个String
值(摘自SessionFactoryBuilderImpl
):
this.schemaAutoTooling = SchemaAutoTooling.interpret( (String) configurationSettings.get( AvailableSettings.HBM2DDL_AUTO ) );
… and internal enum
values of both org.hibernate.boot.SchemaAutoTooling
and org.hibernate.tool.schema.Action
aren't exposed publicly.
......以及enum
两者的内部价值观,org.hibernate.boot.SchemaAutoTooling
并且org.hibernate.tool.schema.Action
不会公开。
Hereunder, a sample programmatic DataSource
configuration (used in ones of my Spring Boot applications) which use a gambit thanks to .name().toLowerCase()
but it only works with values without dash (not create-drop
for instance):
下面是一个示例编程DataSource
配置(在我的 Spring Boot 应用程序中使用),它使用了 gambit,.name().toLowerCase()
但它仅适用于没有破折号的值(不是create-drop
例如):
@Bean(name = ENTITY_MANAGER_NAME)
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier(DATA_SOURCE_NAME) DataSource internalDataSource) {
Map<String, Object> properties = new HashMap<>();
properties.put(AvailableSettings.HBM2DDL_AUTO, SchemaAutoTooling.CREATE.name().toLowerCase());
properties.put(AvailableSettings.DIALECT, H2Dialect.class.getName());
return builder
.dataSource(internalDataSource)
.packages(JpaModelsScanEntry.class, Jsr310JpaConverters.class)
.persistenceUnit(PERSISTENCE_UNIT_NAME)
.properties(properties)
.build();
}
回答by vishal thakur
validate
: validates the schema, no change happens to the database.update
: updates the schema with current execute query.create
: creates new schema every time, and destroys previous data.create-drop
: drops the schema when the application is stopped or SessionFactory is closed explicitly.
validate
: 验证架构,数据库不会发生任何变化。update
:使用当前执行查询更新架构。create
: 每次创建新模式,并破坏以前的数据。create-drop
:当应用程序停止或 SessionFactory 显式关闭时删除架构。