Hibernate - hibernate.hbm2ddl.auto = validate

2020-02-05 06:33发布

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?

1条回答
老娘就宠你
2楼-- · 2020-02-05 07:00

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.

查看更多
登录 后发表回答