views:

28734

answers:

5

I really want to know more about the update, export and the values that could be given to 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 it attributes
  • tables have been dropped
  • values of a column has changed

In each case what is the best solution?

+17  A: 

The configuration property is called 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.

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.

Peter Hilton
+6  A: 

I would use liquibase for 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.

Pat
See http://stackoverflow.com/questions/221379/hibernate-hbm2ddl-autoupdate-in-production for why you shouldn't use hbm2ddl for production.
Nathan Voxland
+4  A: 

Hi,
A while ago I wrote a detailed explanation on my blog, IMO it might help you. Take a look in here.
Eyal

Eyal Lupu
What's your reference for that blog article? Did you inspect the source code (in which case, what class(es) are involved) or did you work it out experimentally?
Andrew Swan
+17  A: 

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

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 at the end of the session.

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?

James McMahon
Just read the hibernate docu ... for valid values, it says: "e.g." ... are there any other valid values?
Not that I am aware of. They could be misusing e.g., or the values could simply be undocumented.
James McMahon
A: 

I was in a great trouble about this. Your post helped me lot. Thanks

semika loku kaluge