Sub topic 7
Using Flyway together with Hibernate
This chapter assumes you have Flyway running at startup in a Spring Boot project, can write versioned SQL migrations, and know basic JPA entity mapping. Here you will decide which tool owns the schema and set the Hibernate and Flyway properties so the two do not conflict.
Two tools, one schema
Hibernate can create and change tables on its own through the spring.jpa.hibernate.ddl-auto property. Flyway also changes tables, through your migration files. If both are allowed to write, the schema depends on which one ran last, and Flyway's history no longer describes the real database.
The rule is simple: Flyway owns the schema, and Hibernate only reads it. Entities describe how Java objects map to tables. They do not define the tables.
| `ddl-auto` value | What Hibernate does at startup | Use with Flyway? |
|---|---|---|
| `none` | Nothing | Yes |
| `validate` | Checks that entities match the tables, and fails startup if not | Yes, recommended |
| `update` | Alters tables to fit the entities | No |
| `create`, `create-drop` | Drops and recreates tables | No |
The configuration
spring:
flyway:
enabled: true
locations: classpath:db/migration
validate-on-migrate: true
jpa:
hibernate:
ddl-auto: validateWith validate, Hibernate compares each mapped entity with the table it expects. A missing table, a missing column, or a column with an incompatible type stops the application from starting. You find out about a mismatch at startup, not when a query fails in production.
Startup order
Spring Boot makes the EntityManagerFactory depend on the Flyway bean. Migrations therefore finish before Hibernate builds its metadata and runs validation.
This order is why validate can be trusted. The tables it checks are the ones Flyway just finished creating.
A worked example
Suppose migration V2__add_display_name.sql added a display_name column to app_user. The entity must map it:
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "app_user")
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true, length = 100)
private String username;
@Column(name = "display_name", length = 200)
private String displayName;
protected AppUser() {
}
public Long getId() { return id; }
public String getUsername() { return username; }
public String getDisplayName() { return displayName; }
}If you add a field to an entity without writing a migration, startup fails with a message like Schema-validation: missing column [nickname] in table [app_user]. The fix is a new migration, V4__add_nickname.sql, not a change to ddl-auto.
Common errors
- Leaving
ddl-auto: updatein place. Hibernate then silently changes tables that Flyway does not know about. Later migrations can fail because the table is not in the state they expect. - Naming mismatches. Spring Boot's default naming strategy turns
displayNameintodisplay_name. A migration that uses another name fails validation unless you set@Column(name = ...). - Type mismatches. An
Integerfield mapped to aBIGINTcolumn can fail validation. Keep entity types and column types aligned. - Treating
validateas a full check. It does not compare indexes, constraints, or default values. Test those separately.
Exercise
A teammate adds a phone field to the AppUser entity and starts the application. Startup fails with a schema-validation error. What is the correct fix, and why is switching ddl-auto to update the wrong one?
Show answer
Write a new versioned migration, for example V4__add_phone.sql with ALTER TABLE app_user ADD COLUMN phone VARCHAR(30);. Flyway then applies it and records it in flyway_schema_history, and validation passes. Switching to update lets Hibernate change the schema outside Flyway. The change is not recorded, it will not be reproduced in other environments, and later migrations may conflict with it.