Junior — Middle
How is the database structure updated when using Hibernate?
sobes.tech AI
Answer from AI
Updating the database schema when using Hibernate is usually done by configuring the hibernate.hbm2ddl.auto property.
Possible values:
validate— checks the consistency between the database schema and the models, without making changes.update— automatically updates the database schema, adding new tables, columns, etc., without deleting existing data.create— recreates the schema, deleting old data.create-drop— creates the schema at startup and drops it at shutdown.
Example configuration in hibernate.cfg.xml or application.properties:
hibernate.hbm2ddl.auto=update
When Hibernate starts, it compares the current models (Entities) with the existing schema and makes changes to align the database with the models.
For more complex schema changes, migration tools (Flyway, Liquibase) are often used, as automatic Hibernate updates are suitable for simple cases and development.