Schema generation vs migrations

Hibernate · lesson 15 of 32 · 3 min read

Use Hibernate DDL for throwaway dev schemas and a migration tool for everything real.

Open this lesson in the learning hub

Key points

  • spring.jpa.hibernate.ddl-auto drives Hibernate DDL: none, validate, update, create, create-drop.
  • update only ever adds. It never drops a column, renames anything or backfills data. It is not a migration tool.
  • In production own the schema with Flyway or Liquibase: versioned scripts, applied in order, recorded in a table.
  • Set ddl-auto=validate in production. Hibernate then checks your mappings against the real schema and fails fast on drift.
  • create-drop is perfect for tests and local demos - point it at Testcontainers, never at data you care about.
  • Ship the migration and the entity change in the same commit. A schema that drifts from the code becomes a 3am page.

Example

# --- application-dev.properties ---------------------------------
# throwaway schema, rebuilt on every start
spring.jpa.hibernate.ddl-auto=create-drop

# --- application-prod.properties --------------------------------
# Flyway owns the schema; Hibernate only checks the mappings match
spring.jpa.hibernate.ddl-auto=validate
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration

# migrations live in src/main/resources/db/migration
#   V1__create_book.sql
#   V2__add_book_published_on.sql

Hibernate can create a schema. Only migrations can evolve one.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Hibernate course, and every lesson in it is listed on the Hibernate contents page.