Hibernation and flying in a production system

I was looking into using hibernate for a project at work. It looks like it does what we want to create a database for a given set of classes. However, it seems highly discouraged to use hbm2ddl.auto = update in production.

Looking back, I don't see what people are doing for this situation. After the database is full and used, we will want to add / update additional persistent classes in the databases without using an unreliable hibernate update.

Flyway looks like it would be useful to handle updating the database schema, but it still requires us to manually create an update script every time we make any code changes.

Is there an easy way to fix this problem automatically? I can't see the hibernation point if it's not good enough for a live update.

Am I missing something?

+3


source to share


1 answer


ORM structures are not suitable for updates because they lack contextual information about schema differences. Therefore, if you rename the column bar

to baz

, all Hibernate will see:

ALTER TABLE foo DROP COLUMN bar;
ALTER TABLE foo ADD COLUMN baz VARCHAR(255) NOT NULL;

      

But you can actually do this:



ALTER TABLE foo ADD COLUMN baz VARCHAR(255);
UPDATE foo SET baz = bar;
ALTER TABLE foo ALTER COLUMN baz SET NOT NULL;
ALTER TABLE foo DROP COLUMN bar;

      

Unless you start explicitly describing your schema changes, no framework will generate the correct DDL updates. And I am not aware of any Hibernate feature for this, so you need Flyway or Liquibase.

+9


source







All Articles