Hibernate hbm2ddl.auto配置的可能值是什么,它们做了什么

本文翻译自:What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto 我真的想知道更多关于更新,导出和可以给hibernate.hbm2ddl.auto的值的更多信息
I need to know when to use the update and when not? 我需要知道何时使用更新,何时不知道? And what is the alternative? 还有什么选择?

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? 在每种情况下,最佳解决方案是什么?


#1楼

参考:https://stackoom.com/question/1pys/Hibernate-hbm-ddl-auto配置的可能值是什么-它们做了什么


#2楼

还有“无”的无证值可以完全禁用它。


#3楼

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] 结帐[SchemaExport]


#4楼

From the community documentation : 来自社区文档

hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. hibernate.hbm2ddl.auto创建SessionFactory时,自动验证或将架构DDL导出到数据库。 With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. 使用create-drop,当SessionFactory显式关闭时,将删除数据库模式。

eg validate | 例如验证| update | 更新| create | 创建| create-drop 创建降

So the list of possible options are, 所以可能的选项列表是,

  • validate : validate the schema, makes no changes to the database. validate :验证模式,不对数据库进行任何更改。
  • update : update the schema. 更新 :更新架构。
  • create : creates the schema, destroying previous data. create :创建架构,销毁以前的数据。
  • create-drop : drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped. create-drop :显式关闭SessionFactory时删除模式,通常是在应用程序停止时。

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? Hibernate:hbm2ddl.auto =在生产中更新?


#5楼

First, the possible values for the hbm2ddl configuration property are the following ones: 首先, hbm2ddl配置属性的可能值如下:

  • none - No action is performed. none - 不执行任何操作。 The schema will not be generated. 不会生成架构。
  • create-only - The database schema will be generated. create-only - 将生成数据库架构。
  • drop - The database schema will be dropped and created afterwards. drop - 之后将删除并创建数据库模式。
  • create - The database schema will be dropped and created afterwards. create - 之后将删除并创建数据库模式。
  • create-drop - The database schema will be dropped and created afterwards. create-drop - 之后将删除并创建数据库模式。 Upon closing the the SessionFactory , the database schema will be dropped. 关闭SessionFactory ,将删除数据库模式。
  • validate - The database schema will be validated using the entity mappings. validate - 将使用实体映射验证数据库模式。
  • update - The database schema will be updated by comparing the existing database schema with the entity mappings. update - 将通过将现有数据库模式与实体映射进行比较来更新数据库模式。

I dedicated a blog post for the most common Hibernate DDL generation strategies : 我为最常见的Hibernate DDL生成策略撰写一篇博客文章

  1. The hibernate.hbm2ddl.auto="update" is convenient but less flexible if you plan on adding functions or executing some custom scripts. 如果您计划添加函数或执行一些自定义脚本, hibernate.hbm2ddl.auto="update"很方便但不太灵活。
  2. The most flexible approach is to use Flyway . 最灵活的方法是使用Flyway

However, even if you use Flyway, you can still generate the initial migration script using hbm2ddl. 但是,即使您使用Flyway,您仍然可以使用hbm2ddl生成初始迁移脚本。 In this article , you can see how you can combine the JPA Entity Model with jOOQ Table Model. 本文中 ,您可以看到如何将JPA实体模型与jOOQ表模型相结合。


#6楼

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. 如果您不想在应用程序中使用字符串并且正在寻找预定义的常量,请查看Hibernate JAR中包含的org.hibernate.cfg.AvailableSettings类,您可以在其中找到所有可能设置的常量。 In your case for example: 以你的情况为例:

/**
 * 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";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值