I want to import some data on the SQL server database, I'm using Spring Boot 2.3.4. I use also Hibernate to generate the tables.
I added flyway core in pom:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
Created the configuration file:
import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@Configuration
public class FlyWayConfiguration {
@Bean
FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, (f) ->{} );
}
@Bean
@DependsOn("entityManagerFactory")
FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, new FlywayMigrationStrategy() {
@Override
public void migrate(Flyway flyway) {
flyway.migrate();
}
});
}
}
I created a file on resources/db/migration/V1_0_1__InitialData.sql
Now I'm having this error
Error creating bean with name 'delayedFlywayInitializer' defined in class path resource
[com/ikun/mkj/config/MigrationConfiguration.class]: Circular depends-on relationship between
'delayedFlywayInitializer' and 'entityManagerFactory' at
org.springframework.beans.factory.support.AbstractBeanFactory
I don't know how to fix this, I searched for solution but couldn't make. Can someone help me please?
Most likely u are deferring the Datasource initializing by adding :
spring.jpa.defer-datasource-initialization =true #set it to false
in your application.[yml/properties].
as in the reference : https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html
set spring.jpa.defer-datasource-initialization to true. This will defer data source initialization until after any EntityManagerFactory beans have been created and initialized. schema.sql can then be used to make additions to any schema creation performed by Hibernate and data.sql can be used to populate it.
And by default Flyway depends on Datasource , Datasource in defer mode will wait for EntityManagerFactory and Ofc since we use flyway the default is to start Flyway before Jpa to ensure DB consistency
So we have a circular Dependency flyway->DS->EMF->Flyway
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With