Blog

Understanding spring.jpa.hibernate.ddl-auto in Spring Boot

When working with Spring Boot and Hibernate, one of the properties you’ll come across is spring.jpa.hibernate.ddl-auto. This property is a powerful tool that controls Hibernate’s automatic schema generation¹³⁶. Let’s dive into what this property does and the differences between its various options.

What is spring.jpa.hibernate.ddl-auto?

The spring.jpa.hibernate.ddl-auto property is a Hibernate feature that Spring Boot utilizes to control how your database schema is managed at the application startup¹³⁶. It can take several values, each of which triggers a different behavior³:

  • create: This value causes Hibernate to drop existing tables, then create new tables based on your entity classes³⁶.
  • create-drop: Similar to create, but it also drops the tables when the SessionFactory is closed, typically when the application shuts down³⁶.
  • update: With this value, Hibernate will make any necessary changes to the database schema to align it with your entity classes³⁶.
  • validate: This value makes Hibernate check that your database schema matches your entity classes, but it won’t make any changes³⁶.
  • none: No action will be performed on the database schema³⁶.

create vs update

While create and update might seem similar, they have a key difference. The create option is more destructive as it will drop all existing tables and recreate them³⁶. This means all your data will be lost every time you start the application³⁶.

On the other hand, update is more conservative. It only makes changes to the database schema to align it with your entity classes³⁶. Existing data is preserved, and only the schema is updated³⁶.

When to use which option?

The choice of value for spring.jpa.hibernate.ddl-auto depends on your development stage and requirements¹³⁶:

  • create or create-drop can be useful during the initial development stages or for integration tests, where you want a clean database for each run¹³⁶.
  • update is helpful during development when you want to preserve your data but accommodate changes in your entity classes¹³⁶.
  • validate or none are typically used in production environments where you don’t want Hibernate to make any changes to your database schema¹³⁶.

The spring.jpa.hibernate.ddl-auto property can only be set to one value at a time. However, if you want to use both create and update, you can manage this manually by changing the property value as needed.

Here’s a typical workflow:

  1. Development Phase: Set spring.jpa.hibernate.ddl-auto=create during the initial development phase. This will drop existing tables and create new ones each time you start the application. It’s useful when your domain models (@Entity classes) are changing frequently, and you want the database schema to be automatically updated to match.
  2. Testing/Production Phase: Once your schema is stable, you can change spring.jpa.hibernate.ddl-auto to update. This will only make changes to the database schema if your domain models change, but it won’t drop existing tables. This is useful when you want to preserve your data but still accommodate minor changes in the domain models.

Remember to be careful with these settings, especially when dealing with production databases. It’s generally not recommended to use create or update in a production environment because it can lead to data loss if not managed carefully. In production, it’s usually better to manage database schema changes manually or use a database migration tool.

I hope this helps! Let me know if you have any other questions. 😊

Conclusion

Understanding the spring.jpa.hibernate.ddl-auto property is crucial when working with Spring Boot and Hibernate. It gives you control over your database schema generation, helping you manage your database in different environments and stages of development.


I hope this helps! Let me know if you need any changes or additions. 😊

Source: Conversation with Copilot, 29/5/2024
(1) java – How does spring.jpa.hibernate.ddl-auto property exactly work in …. https://stackoverflow.com/questions/42135114/how-does-spring-jpa-hibernate-ddl-auto-property-exactly-work-in-spring.
(2) Spring JPA Hibernate DDL Auto: What It Is and How to Use It. https://hatchjs.com/spring-jpa-hibernate-ddl-auto/.
(3) Guide on Loading Initial Data with Spring Boot | Baeldung. https://www.baeldung.com/spring-boot-data-sql-and-schema-sql.
(4) 59. Database initialization – Spring | Home. https://docs.spring.io/spring-boot/docs/1.1.0.M1/reference/html/howto-database-initialization.html.
(5) Spring Boot Database Initialization | SpringHow. https://springhow.com/spring-boot-database-initialization/.
(6) Spring Boot 3 Crash Course Part 3: Hibernate and Persist. https://medium.com/@ben.meehan_27368/spring-boot-3-crash-course-part-3-hibernate-and-persist-66b853f8cb20.

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over