What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Hibernate, one of the core features managing the schema of the underlying database is configured through the hbm2ddl.auto property. This configuration property is crucial for developers and administrators, as it defines how Hibernate interacts with the database schema at the system initialization time. Below, we delve into the different values that can be assigned to the hbm2ddl.auto property, each serving distinct purposes depending on the development stage or the production requirements.
Understanding hbm2ddl.auto
The hbm2ddl.auto property automatically validates or exports schema DDL (Data Definition Language) to the database when the SessionFactory is created. Essentially, it can manage the schema update of the database according to the mapping definitions provided by the developer. Here are the common values that can be set for hbm2ddl.auto:
nonevalidateupdatecreatecreate-drop
none
When this value is set, no action will be taken on the database schema. This means Hibernate will not interact with the structure of the database, and it's up to the administrator or an external process to manage the schema.
validate
With this configuration, Hibernate will validate the schema against the mappings. This means it checks whether the tables and columns exist with the appropriate type, length, etc., as specified in your mappings (annotations or XML). If there is a discrepancy between the Java entities and the actual database schema, it throws an exception, signaling that the schema needs to be corrected.
update
This is probably the most commonly used mode during development. If update is set, Hibernate will update the schema based on the entities defined in your project. It will add new tables, columns, and constraints but does not remove any existing ones. However, updating a live database can potentially lead to loss of data, and therefore, it is not recommended for production environments.
create
Setting hbm2ddl.auto to create will create the schema every time the SessionFactory is started, dropping the existing schema first if it exists. This is useful in development or testing environments where the database is rebuilt from scratch frequently.
create-drop
With create-drop, the schema will be dropped when the SessionFactory is closed explicitly, typically when the application is stopped. This is particularly useful for integration tests where each test is required to run in isolation.
Technical Examples and Use Cases
For instance, in a Java-based configuration, hbm2ddl.auto can be set as follows:
Best Practices and Considerations
While hbm2ddl.auto provides significant flexibility, it must be used with caution, especially in production environments:
- Never use
createorcreate-dropin a production environment, as they will lead to complete data loss. - Use
updatewith caution; it's more appropriate for development, not for production. validatecould be beneficial for a production environment since it simply verifies the schema's correctness against the mappings without making changes.- Always back up your data before running any schema changes.
Summary Table
| Value | Description | Recommended Environment |
none | No action will be taken. | Production |
validate | Validates the database schema against the Hibernate mappings. | Production |
update | Updates the schema by adding new entities and columns, without deleting existing data. | Development |
create | Creates the database schema afresh every time, and deletes the old one. | Development, Testing |
create-drop | Creates the schema and drops it when the SessionFactory is closed. | Testing |
Implementing the proper hbm2ddl.auto configuration is crucial for effective database and schema management in Hibernate-based applications, influencing both development practices and production system behavior.

