What Are the Possible Values of the Hibernate Hbm2Ddl.Auto Configuration and What Do They Do

Hibernate hbm2ddl.auto, possible values, and what they do

The link you provided is already the official documentation. So, there's nothing more official and comprehensive as-of today.

So I guess the answer to your question is two-fold:

  • either file an enhancement request in the Hibernate issue tracker (better with a proposal)
  • or read the corresponding code

I know this isn't the perfect answer you dreamt about, but this is actually all you have today.

But the good news is that the project is open-source, so you have all you need to help improve it :-).

Hibernate hbm2ddl.auto default value

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.

validate | update | create | create-drop
  • validate- existing schema
  • update- only update your schema once created
  • create- create schema every time

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.

How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?

For the record, the spring.jpa.hibernate.ddl-auto property is Spring Data JPA specific and is their way to specify a value that will eventually be passed to Hibernate under the property it knows, hibernate.hbm2ddl.auto.

The values create, create-drop, validate, and update basically influence how the schema tool management will manipulate the database schema at startup.

For example, the update operation will query the JDBC driver's API to get the database metadata and then Hibernate compares the object model it creates based on reading your annotated classes or HBM XML mappings and will attempt to adjust the schema on-the-fly.

The update operation for example will attempt to add new columns, constraints, etc but will never remove a column or constraint that may have existed previously but no longer does as part of the object model from a prior run.

Typically in test case scenarios, you'll likely use create-drop so that you create your schema, your test case adds some mock data, you run your tests, and then during the test case cleanup, the schema objects are dropped, leaving an empty database.

In development, it's often common to see developers use update to automatically modify the schema to add new additions upon restart. But again understand, this does not remove a column or constraint that may exist from previous executions that is no longer necessary.

In production, it's often highly recommended you use none or simply don't specify this property. That is because it's common practice for DBAs to review migration scripts for database changes, particularly if your database is shared across multiple services and applications.

hibernate.hbm2ddl.auto configuration

hibernate.hbm2ddl.auto=create will drop and create only tables that are mapped by your hibernate instance. All other tables (unmapped) will be untouched.



Related Topics



Leave a reply



Submit