3

I read the discussion about using hbm2ddl.auto=update in order to auto-update changes to the database schema.

The thread is from 2008 and I do not know how secure it is to use the auto-update mode today.

We are running a small JavaEE on a Glassfish with Hibernate 4.3.11 and PostgreSQL. We plan to use continious integration with Jenkins.

Is it useful to work with hbm2ddl.auto=update enabled? Or is it better to use an easy alternative to update/check the updates maybe manually?

I know it is hard to give a blanket statement.

Community
  • 1
  • 1
Marcel
  • 1,537
  • 5
  • 19
  • 38

1 Answers1

6

You should not use hbm2ddl.auto=update to update production databases.

Few reasons:

  • Hibernate will only INSERT missing columns and not modify existing columns. Therefore, if you rename a property (Client to Customer), Hibernate will create a new column Customer, leaving the column Client untouched. You will need to manually "move" the data there and remove the orphan column.
  • Hibernate will not remove constraints on no longer mapped columns. Thus, if your Client column was NOT NULL, any insert query to that table will now fail in the first place, because Hibernate won't provide any data for the orphan column (Which still has it's NOT NULL constraint) anymore.
  • Hibernate will not touch data types of existing columns. So, if you change a property type from String to Date - Hibernate will leave the column definition as varchar.
  • Hibernate does not remove columns of which you deleted the property, leading to data-polution and worst-case (The constraints remain in place) to no longer working applications.
  • If you create additiional constriants on existing columns - hibernate will not create them, because the column already existed before. (You might miss important contraints on the production db you added on existing columns)

So, perform your updates on your own is safer. If you have to take into account what hibernate is doing and what not - you'd better do it on your own from the scratch.

dognose
  • 20,360
  • 9
  • 61
  • 107
  • Thanks for your answer. How do I perform those updates manually? Do I have to alter each table by my own with SQL statements or similar? – Marcel Sep 17 '15 at 15:44
  • @Marcel IT depends. We have a versioned SQL-file containing all the required alter-Statements in the correct order. This is okay, if the database is unlikely to change, which mithgt not be the case during early stages of the application. – dognose Sep 17 '15 at 15:46
  • @dognose would you mind sharing the source of your answer? – Yoshua Nahar Nov 03 '17 at 09:28
  • @YoshuaNahar the official documentation is quite short on this topic, I figured it out the "hard way" as well. However, this tutorial here also mentions that "update" will only affect non-existing tables and/or columns: http://www.onlinetutorialspoint.com/hibernate/hbm2ddl-auto-example-hibernate-xml-config.html – dognose Nov 06 '17 at 11:50