Hibernate / Hibernate 7 Basics Interview Questions
How does Hibernate 7 handle schema validation and generation (hbm2ddl)?
Hibernate can validate or generate the DB schema from entity mappings. Controlled by hibernate.hbm2ddl.auto.
| Value | Behaviour | Use in |
|---|---|---|
| none | No schema action | Production (use Flyway/Liquibase) |
| validate | Validates entity mappings match schema; fails on mismatch | Production safety check |
| update | Adds missing columns/tables; never drops | Development (risky in production) |
| create | Drops and recreates all tables on startup | Development/testing |
| create-drop | Creates on startup; drops on SessionFactory close | Unit tests |
# Recommended production setup: spring: jpa: hibernate: ddl-auto: none # Hibernate does NOT touch schema flyway: enabled: true # Flyway manages schema with versioned migrations locations: classpath:db/migration # Development: spring: jpa: hibernate: ddl-auto: create-drop # fresh schema each run # Flyway migration files: # db/migration/V1__create_products_table.sql # db/migration/V2__add_category_to_products.sql
More Related questions...