37

My database background comes from the Django framework (python). In Django, getting started with database migrations was easy: Django migrations. The Django framework provided tool for creating the migrations based on your models, and also a tool to apply the migrations on your database. I think this way of doing worked in both development and in production. You did not have to write migrations by yourself, the framework created them for you.

Now I have started a project with Spring Boot and Hibernate. I configure my application to use hibernate with JPA. With these settings, I now would need to know how does my framework handle database migrations? I mean if I change a column, either it's type, or even might remove it, then how do I migrate the database to the change? I know that spring boot will automatically detect column changes on startup, and create columns that do not exist based on the models (Entity's). I guess it has something to do with variable

spring.jpa.hibernate.ddl-auto

But how does it handle the existing database objects? Does it add the column to them too, and with what value? The default value I set? What if I change the column type? Can it then handle the change? These settings and spring-boot automated database management probably are not enough in the long run?

What I want to know is, that what are the best practices on how to handle database migrations with Spring Boot and hibernate combination? I believe there is a standard how most of the people with this combination handle the migrations? I hope it is as easy as with Django... I know about flyway, but don't know if I really need it, or if it is used much with this combination of mine (including spring boot and hibernate).

2
  • 7
    Yes, Flyway and liquibase are both good database migration tools. The fact that you're using spring and hibernate is really irrelevant. If you need to migrate a database, they're good choices. ddl-auto is not a database migration tool. It just is a quick way of getting started and generate a schema from scratch, IMHO. Not something I'd ever use in production. Commented Jun 4, 2017 at 10:04
  • hey whats the need of migration tool if ,we can create database and stuffs using hibernate-jpa Commented Mar 22, 2021 at 13:12

1 Answer 1

21

Liquibase or Flyway are the two main options for versioning/handling database migrations. ddl-auto is quick and dirty, but it does not, nor can it, take into account everything that needs to be handled. THere's also the chance of race-conditions (two instances trying to update the DDL at the same time).

This answer goes into more detail about ddl-auto in a production environment, and why you shouldn't.

Hibernate: hbm2ddl.auto=update in production?

https://www.credera.com/blog/technology-insights/java/liquibase-fed-inconsistent-schemas/ has bit more info on the why/concepts.

Sign up to request clarification or add additional context in comments.

2 Comments

hey whats the need of migration tool if ,we can create database and stuffs using hibernate-jpa
Have you read the related question stackoverflow.com/questions/221379/… which should answer your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.