Reputation: 143
I'm having issues with getting flyway to play well with hibernate. I used to have the standard flyway settings, and it would run first on app startup. Great, exactly what I wanted.
Hibernate settings has spring.jpa.hibernate.ddl-auto=none
I then changed my main application to run flyway explicitly (because I want to conditionally run flyway in certain scenarios):
@Configuration
public class EmptyMigrationStrategyConfig {
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
// do nothing
};
}
}
@SpringBootApplication(exclude = {R2dbcAutoConfiguration.class})
@Configuration
@EnableRetry
public class MainApplication implements CommandLineRunner {
@Autowired
private Flyway flyway;
@Autowired
private ShutdownManager shutdownManager;
@Value("#{new Boolean('${app.migrations-only}')}")
boolean migrationsOnly;
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
flyway.migrate();
if (migrationsOnly) {
shutdownManager.initiateShutdown(0);
}
}
}
But now if I create a new migration script and add a POJO for an entity, the app fails to boot because hibernate is failing since flyway is no longer running first.
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: <entity> at <table>
What do I need to define so things run in the proper order?
Upvotes: 2
Views: 1513
Reputation: 143
Ok so the actual issue ended up being a typo in the hibernate model... regardless this is how you'd get flyway to run explicitly first:
@Configuration
public class EmptyMigrationStrategyConfig {
@Autowired
private ShutdownManager shutdownManager;
@Value("#{new Boolean('${app.migrations-only}')}")
boolean migrationsOnly;
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
flyway.migrate();
if (migrationsOnly) {
shutdownManager.initiateShutdown(0);
}
};
}
}
@SpringBootApplication(exclude = {R2dbcAutoConfiguration.class})
@Configuration
@EnableRetry
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Upvotes: 2