Spring Boot Hibernate and Flyway boot order
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In a typical Spring Boot application, Flyway should migrate the database before Hibernate starts validating or using the schema. That boot order matters because your entity mappings are written against the expected current schema, not against yesterday's schema. If Hibernate starts too early, you get validation failures, missing-table errors, or misleading startup behavior.
The Normal Boot Order
In the usual Spring Boot setup with one main datasource:
- the datasource is created
- Flyway runs pending migrations
- JPA and Hibernate initialize against the migrated schema
That is the behavior most teams want. Flyway updates the database structure, then Hibernate validates or uses it.
A common configuration looks like this:
Using ddl-auto=validate is important in Flyway-managed systems because it tells Hibernate to verify the schema instead of trying to create or modify it itself.
Why Flyway Should Go First
Flyway is the schema authority. Hibernate should not be inventing schema changes in production when Flyway is already tracking them as versioned migrations.
For example, suppose you add a new email column to a users table and your entity now expects it:
If Flyway has not yet applied the migration, Hibernate can fail during startup because the database does not match the entity mapping.
That is why the desired sequence is always migration first, ORM second.
A Good Flyway Workflow
A typical Flyway migration might be:
Then on startup:
- Flyway applies
V3 - Hibernate validates that
emailexists - the application starts cleanly
This is far more predictable than relying on ddl-auto=update, which can make uncontrolled schema changes and drift away from your audited migration history.
What Can Disrupt the Expected Order
The normal order is reliable in the common case, but you can disrupt it with custom configuration.
Examples include:
- multiple datasources with custom bean wiring
- manually constructed
EntityManagerFactorybeans - disabling Flyway accidentally in one environment
- using SQL init scripts and Flyway together without a clear strategy
If you step outside Spring Boot's default autoconfiguration path, verify the bean dependencies explicitly. The more custom the startup configuration becomes, the less you should assume the default ordering still protects you.
ddl-auto Choices With Flyway
When Flyway manages schema changes, the most common Hibernate settings are:
- '
validatein production and often in development' - '
nonein some specialized setups'
Usually avoid:
- '
updatebecause it competes with Flyway for schema ownership' - '
createorcreate-dropexcept for throwaway test environments'
If both Flyway and Hibernate try to mutate the schema, you lose the clarity and reproducibility Flyway is supposed to provide.
Testing the Startup Contract
A simple integration test can confirm that migrations run before your repositories and entity manager are used.
The value of the test is not the assertion itself. It is the fact that the application context must complete startup successfully with Flyway and JPA configured together.
Common Pitfalls
The most common mistake is using Flyway for migrations while leaving spring.jpa.hibernate.ddl-auto=update. That creates two schema managers with conflicting responsibilities.
Another mistake is assuming Boot order will stay correct after introducing multiple datasources or custom JPA beans. Custom wiring can break the default lifecycle.
Developers also sometimes disable Flyway in one environment and then wonder why Hibernate validation fails there but not elsewhere.
Finally, remember that startup order is not the whole story. Your migration scripts still need to be correct, idempotent where appropriate, and aligned with entity changes.
Summary
- In a normal Spring Boot setup, Flyway should run before Hibernate initializes the schema.
- Use Flyway as the source of truth for schema changes and let Hibernate validate.
- '
spring.jpa.hibernate.ddl-auto=validateis usually the right companion setting.' - Custom datasources and bean wiring can break the expected default ordering.
- If Flyway manages the schema, avoid letting Hibernate mutate it independently.
Related reading
- Spring Boot JPA2 Hibernate - enable second level cache
- Spring Boot JPA - configuring auto reconnect
- Spring Boot JPA Column name annotation ignored
- Spring Boot JPA Column name annotation ignored
- Spring boot hikari - dataSource or dataSourceClassName or jdbcUrl is required issue
- Spring Boot hotswap with IntelliJ IDE
- Spring Boot Multiple Datasource
- Spring Boot PSQLException FATAL sorry, too many clients already when running tests

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.