Hibernate hbm2ddl.auto possible values and what th

2018-12-31 00:37发布

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?

These are changes that could happen over 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?

13条回答
ら面具成の殇う
2楼-- · 2018-12-31 00:59

There's also the undocumented value of "none" to disable it entirely.

查看更多
长期被迫恋爱
3楼-- · 2018-12-31 01:01

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]

查看更多
临风纵饮
4楼-- · 2018-12-31 01:04

validate: validate the schema, do not change happens to the database. update: update the schema with currently execute query. create: creates new schema every time, and destroy previous data. create-drop: drop the schema when the application is stopped or SessionFactory is closed explicitly.

查看更多
只若初见
5楼-- · 2018-12-31 01:06

validate: It validates the schema and makes no changes to the DB.
Assume you have added a new column in the mapping file and perform the insert operation, it will throw an Exception "missing the XYZ column" because the existing schema is different than the object you are going to insert. If you alter the table by adding that new column manually then perform the Insert operation then it will definitely insert all columns along with the new column to the Table. Means it doesn't make any changes/alters the existing schema/table.

update: it alters the existing table in the database when you perform operation. You can add or remove columns with this option of hbm2ddl. But if you are going to add a new column that is 'NOT NULL' then it will ignore adding that particular column to the DB. Because the Table must be empty if you want to add a 'NOT NULL' column to the existing table.

查看更多
看风景的人
6楼-- · 2018-12-31 01:09

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.

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.

查看更多
无与为乐者.
7楼-- · 2018-12-31 01:13

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:

/**
 * 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";
查看更多
登录 后发表回答