Hibernate: Hbm2Ddl.Auto=Update in Production

Hibernate: hbm2ddl.auto=update in production?

No, it's unsafe.

Despite the best efforts of the Hibernate team, you simply cannot rely on automatic updates in production. Write your own patches, review them with DBA, test them, then apply them manually.

Theoretically, if hbm2ddl update worked in development, it should work in production too. But in reality, it's not always the case.

Even if it worked OK, it may be sub-optimal. DBAs are paid that much for a reason.

hibernate.hbm2ddl.auto create-drop with production Database


  1. No. The default is fine: it does nothing with the database
  2. Well, it will drop your database (you'll lose everything) when the session factory is closed, and recreate the schema when it restarts. So you really, really don't want that in production.

If you want help on your exception, you should show your code, show the stack trace of the exception, and show the relevant information (what the relevant tables look like, for example). I doubt hbm2ddl.auto has anything to do with that exception. Usually, when an exception occurs, it's a problem with your code, not a problem with hibernate. That's true of any framework/library.

Hibernate hbm2ddl.auto create/update skip View

In this case I would use @Subselect instead of @Table (https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/annotations/Subselect.html)

As for documentation @Subselect

Map an immutable and read-only entity to a given SQL select
expression.

hbm2ddl.auto is update but still creating the table in the database

If you want Hibernate to do nothing to your schema, set hbm2ddl.auto to none.

Which value of hibernate.hbm2ddl.auto property should be used in production environment?

You should set hibernate.hbm2ddl.auto to validate in all your environments/profiles.

You might want to check Liquibase or Flyway to update your DB in running envs and for integration testing.

meaning of update when value of hbm2ddl.auto set to update

Please refer to the attached image, tried documented as much as i can.

Sample Image

Hibernate : hbm2ddl.auto value= update or none

It is not good for many reasons, especially in production environments.

  • Versioning - you may want to version your database, most people do
    manually by keeping track of db changes, it's good to prepare a revert script for every change. It's important on the version release to have
    a rollback plan in case things go bad.

  • Script execution failure - In a database full of data, an alter table which
    adds a non-null column will fail, simply because existing records do
    not have this column. So there are additional steps which are best handled
    manually.

  • Accidental changes. A developer from your team may accidentally
    commit wrongly configured entities and wreak havoc on your schema.

The best is to keep the value of hibernate.hbm2ddl.auto to validate, this way entity mappings are validated against database structure, and if there's any mismatch, Hibernate will warn.



Related Topics



Leave a reply



Submit