如何在第一次在hibernate中创建数据库模式并在模式修改的情况下进一步更新? [英] How to create database schema in hibernate first time and further update it in case of schema modification?

查看:177
本文介绍了如何在第一次在hibernate中创建数据库模式并在模式修改的情况下进一步更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在休眠第一次创建数据库模式。此外,如果模式中有任何修改,例如添加新表或删除某一列,我想更新现有模式,保持以前的数据不变。



按照这个问题,它看起来像我可以创建架构销毁以前的数据,或者我可以更新架构。



有什么值可以兼得吗?

解决方案

其实我只是检查了< property name =hibernate.hbm2ddl.autovalue =update/> ; 甚至第一次创建表,然后如果表/模式存在,它会更新。


$ b

更新属性在启动或添加新模型时适用。您想保留先前保存的实体实例。这是默认的模式创建风格。



如果需要,它会尝试更新模式。支持以下更新:



查看我的一些观察结果


  • 添加字段 - 将新列添加到表中。

  • 重命名字段 - 将新列添加到表中,而原始列保留但不再使用。 注意:旧列中的数据不会迁移到新列。 删除一个字段 - 列保留但未使用。

  • 更改字段类型 - 列的类型不会更改,这可能会导致类型不匹配的异常。

  • 创建实体 - 创建一个新表。

  • 重命名一个实体 - 创建一个新表,并保留原始表。

  • 将实体移动到另一个文件夹 - 创建一个新表格,而原始表格仍然存在。

  • 删除实体 - 表格仍然存在。


I want to create database schema in hibernate first time. And further, if there is any modification in the schema, like addition of a new table or deletion of some column, I want to update the existing schema keeping the previous data intact.

As per options given at this question, it looks like either I can create schema destroying the previous data, or I can update the schema.

Is there any value which can do both?

解决方案

Actually I just checked <property name="hibernate.hbm2ddl.auto" value="update" /> is even creating tables for the first time and then later if table/schema exist it does update.

Update property is applicable when starting or adding a new model. You want to retain the earlier saved entity instances. This is the default schema creation style.

It tries to update the schema, if required. The following updates are supported:

See some of my observations

  • Add a field - A new column is added to the table.
  • Rename a field - A new column is added to the table, while the original column remains but is not used any longer. Note: The data from the old column is not migrated to the new column.
  • Remove a field - The column remains but is not used.
  • Change a field type - The type of the column does not change, which may result in type-mismatch exceptions.
  • Create an entity - Creates a new table.
  • Rename an entity - Creates a new table, while the original table remains.
  • Move an entity to another folder - Creates a new table, while the original table remains.
  • Delete an entity - The table remains.

这篇关于如何在第一次在hibernate中创建数据库模式并在模式修改的情况下进一步更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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