Menu

Schema Generation and Migrations

Schema Generation and Migrations

Generating a Schema

Hibernate can create or update tables from the entity mappings. The behaviour is set with hibernate.hbm2ddl.auto.

Value

Effect

noneDoes nothing
validateChecks that the schema matches the mappings and fails if not
updateAdds missing tables and columns
createDrops and recreates the schema at startup
create-dropCreates at startup and drops when the factory closes
hibernate.hbm2ddl.auto=create-drop

Code Explanation:

  • create-drop suits demos and tests with an in-memory database, since it always starts from an empty schema.
  • Changing the value to validate makes Hibernate only check the existing schema.

Migrations in Real Projects

update never removes or renames columns, and it cannot be reviewed or rolled back. It is not safe for production.

Real applications manage the schema with a migration tool such as Flyway or Liquibase. Each schema change is a versioned script kept in source control, and Hibernate is set to validate or none.


Hibernate Tutorial

AP

Abhishek Pati



Learn via Video Course

Java Programming ( Telugu) Logo

Java Programming ( Telugu)

11 hrs



Get in Touch For Details