Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - hibernate.hbm2ddl.auto = validate

I am interested in how hibernate.hbm2ddl.auto=validate actually works and I am struggling to find comprehensive documentation.

We've recently discovered production system was affected by http://opensource.atlassian.com/projects/hibernate/browse/HHH-3532 (Hibernate matches foreign keys on name, rather than signature and so will recreate them for you) and hibernate.hbm2ddl.auto=update is being removed from our next release.

I would be quite happy to just get rid of hibernate.hbm2ddl.auto altogether and manage our database ourselves. However, not all my colleagues share this world view and some are keen to add back in hibernate.hbm2ddl.auto=validate.

I'm concerned this will hit the same problem and I'm interested in finding more documentation about how this validation actually works. The Hibernate Community Documentation (http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html) really just makes reference to the values.

Does anyone have any good documentation pointers, or any real life experience of using validate in a production system?

like image 880
azp74 Avatar asked Apr 23 '10 02:04

azp74


People also ask

What is hbm2ddl Auto in 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.

What is the default value for Hibernate hbm2ddl Auto?

hbm2ddl. auto defaults to Hibernate not doing anything. Show activity on this post. Automatically validates or exports schema DDL to the database when the SessionFactory is created.

Is hbm2ddl a Hibernate tool?

First of all, the hbm2ddl. auto schema generation tool is very useful for the Hibernate project because it allows creating integration tests that can run on any of the supported relation database systems.

When we set create-drop value for hbm2ddl auto property when Hibernate will drop the table?

auto Create : If the value is CREATE then the hibernate first drops the existing tables data and structure, then creates new tables and executes the operations on the newly created tables. The only problem with the value “create” is, we lose existing table data.


1 Answers

I'm concerned this will hit the same problem and I'm interested in finding more documentation about how this validation actually works.

In my opinion, the best documentation is the source code that you could check to see what is happening exactly. The relevant method is org.hibernate.tool.hbm2ddl.SchemaValidator#validate().

I went quickly through the code and I don't think that the SchemaValidator verifies foreign keys in the database: it checks the presence of tables, columns, id generators but not foreign keys. A test against a pet database seems to confirm this behavior: dropping a FK constraint doesn't break schema validation (in other words, the validator checks if the application can run, not for referential integrity).

Now, HHH-3532 being marked as fixed, why don't you upgrade to a newer version of Hibernate or, if changing the version of Hibernate is too heavy, why don't you apply the patch for HHH-3532 yourself?

Having that all said, I don't use hibernate.hbm2ddl.auto=update to update production databases, I use change scripts. But I use hibernate.hbm2ddl.auto=validate and I'm happy with it.

like image 91
Pascal Thivent Avatar answered Oct 26 '22 14:10

Pascal Thivent