The hibernate.hbm2ddl.auto property controls whether Hibernate creates, updates, validates or drops your database schema when the SessionFactory starts. It is convenient for prototypes and tests, but only validate or none are safe choices for production, where schema changes should come from a migration tool such as Flyway or Liquibase.
In Spring Boot you set the same behaviour with spring.jpa.hibernate.ddl-auto. In Jakarta Persistence 3.x there is also a standard setting, jakarta.persistence.schema-generation.database.action.
At startup Hibernate knows your entity mappings. With schema management enabled it compares them to the database and runs DDL statements: create table, alter table, drop table and so on. The hbm2ddl name comes from the old .hbm.xml mapping files, but it applies to annotated entities as well.
The property accepts these values:
| Value | Behaviour |
|---|---|
none | Does nothing. Schema is managed elsewhere. |
validate | Checks that tables and columns match the mappings and fails startup if not. Changes nothing. |
update | Adds missing tables and columns. Never drops or renames anything. |
create | Drops existing objects, then creates the schema. Data is lost on every start. |
create-drop | Like create, and drops the schema when the SessionFactory closes. |
create-only | Only generates the create statements, without dropping first (Hibernate 5.x and later). |
drop | Only drops the schema objects. |
In current Hibernate versions the values create-only and drop are also available through the JPA setting jakarta.persistence.schema-generation.database.action, whose standard values are none, create, drop-and-create and drop.
In persistence.xml:
In Spring Boot, use the Boot property, which is passed through to Hibernate:
Spring Boot defaults to create-drop for an embedded database such as H2 or HSQLDB when no schema manager like Flyway or Liquibase is detected, and to none otherwise. Relying on a default that depends on the classpath is risky, so set the value explicitly per profile:
Consider this entity:
With create, Hibernate runs statements similar to the following (the exact SQL depends on the dialect):
If you later add a phone field, update runs alter table customer add column phone varchar(255), while validate fails with a SchemaManagementException such as Schema-validation: missing column [phone] in table [customer].
update looks like a migration tool, but it is not one:
Use a versioned migration tool and let Hibernate only verify the result:
spring.jpa.hibernate.ddl-auto=validate so a mismatch between code and schema stops the application at startup instead of failing later at runtime.To bootstrap the first migration, you can ask Hibernate to write a script instead of running it:
Review the generated file and commit it as a migration rather than trusting generation at runtime.
Both rebuild the schema at startup. create-drop also drops it when the SessionFactory is closed, which suits short-lived tests.
Yes in effect. Spring Boot's property sets hibernate.hbm2ddl.auto for you, and has its own defaults when you do not set it.
No. It only reports differences and stops startup. You must change the database or the mapping.
The value was probably create or create-drop, often by Spring Boot's embedded-database default. Set the property explicitly to none or validate.
hibernate.hbm2ddl.auto decides whether Hibernate changes the schema at startup. Use create or create-drop for tests, update only for throwaway development, and validate or none in production with Flyway or Liquibase handling versioned migrations. Always set it explicitly so defaults on the classpath cannot surprise you.