How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In a Spring Boot application that interacts with databases through Hibernate, you will likely encounter the configuration property spring.jpa.hibernate.ddl-auto. This property is pivotal in defining the behavior of Hibernate Database Schema Management, commonly determining how and when the database schema should be updated from the domain models.
Understanding spring.jpa.hibernate.ddl-auto
The spring.jpa.hibernate.ddl-auto property is a setting in Hibernate used to manage the Data Definition Language (DDL) operations, particularly the schema generation and updating. The property allows you to specify what action Hibernate should perform regarding the database schema when the Hibernate SessionFactory is created or updated. There are several values that this property can take:
- none: No action will be taken, meaning no DDL operations will be performed automatically.
- validate: Hibernate will validate that the tables and columns exist and match the entity mappings. If discrepancies are found, an exception is thrown.
- update: Hibernate updates the schema to reflect the entities in the model without dropping any tables or data.
- create: All database schemas are dropped upon startup if they exist, and then the schema is recreated from the current model.
- create-drop: Similar to
create, but additionally, when theSessionFactoryis closed, typically when the application shuts down, everything is dropped.
Examples and Application Scenarios
Understanding when and how to use these values is crucial for effective application behavior. Here are some exemplified scenarios:
- During development, setting it to
create-dropallows you to freely modify entities without worrying about the state of the database, as it resets on restart. - For Quality Assurance environments, you might use
updateso that changes are reflected without losing any test data. - In production, it is recommended to use
validateto ensure your Entity mappings are aligned with the database schema without making any changes to the schema itself.
Impact of Different Settings
Here’s a quick rundown of how each setting impacts application deployment and development:
| Setting | Description | Use Case |
| none | Does nothing specific with the database schema. | Production environments |
| validate | Validates the schema, throws an error if mismatched. | Pre-production/Production |
| update | Updates the schema while keeping existing data. | Development/QA environments |
| create | Creates the schema, destroying previous data. | Initial development cycles |
| create-drop | Creates and then deletes the schema on session end. | Testing environments with in-memory DBs |
Technical Integration
In practice, setting this property in Spring Boot can be done via application.properties or application.yml. For instance:
Or, in YAML format:
Considerations
While the ddl-auto setting is useful, it should be used with various considerations:
- Data Integrity: Especially with
createandcreate-drop, data is not preserved. Ensure data that shouldn't be lost is backed up or that these settings are only used in appropriate environments. - Performance: Using
updatein production, especially with large schemas, might affect startup times and performance. Always validate such settings in a staging environment before production use. - Version controls: Schema changes should ideally be version-controlled using migration tools like Flyway or Liquibase, which gives more control and stability over the database schema across environments.
Conclusion
In summary, spring.jpa.hibernate.ddl-auto is a powerful property in Hibernate and Spring Boot frameworks, offering a wide range of database schema management policies tailored to different stages of application development and deployment. Understanding and configuring it according to application lifecycle requirements ensures both optimal performance and data integrity.

