Spring Boot: Hibernate and Pass Boot

I created a Spring application. Pom xml attached.

It has a configuration similar to this one (below), and some db / migration / V1__init.sql for the Flyway migration migration tool.

It has an hsqldb database in memory and is created after the application starts. It is clean after creation.

I want Hibernate to create a schema based on entity classes, and then Flyway will populate the tables. Now Flyway launches V1__init.sql before creating tables and throws an exception. How can I change this order or what decision can I make?

spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = create-drop spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect 

pom.xml:

 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.11.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>2.1.4.RELEASE</version> </dependency> <!-- For using 'LEGACYHTML5' mode in Thymeleaf --> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.21</version> </dependency> <dependency> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> <version>1.4.01</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.3.3.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 
+13
source share
4 answers

I had the same problem.

I wanted my schema to be created by hibernate because of its independence from the database. I already faced the problem of coming up with a good scheme for my application in my jpa classes, I do not like to repeat myself.

But I want some data initialization to be done in a versioned way that is good for flying.

Spring loading starts the migration of the flyway to hibernation. To change it, I overloaded the boot spring initializer to do nothing. Then I created a second initializer that starts after hibernation ends. All you have to do is add this configuration class:

 import org.flywaydb.core.Flyway; import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; @Configuration public class MigrationConfiguration { /** * Override default flyway initializer to do nothing */ @Bean FlywayMigrationInitializer flywayInitializer(Flyway flyway) { return new FlywayMigrationInitializer(flyway, (f) ->{} ); } /** * Create a second flyway initializer to run after jpa has created the schema */ @Bean @DependsOn("entityManagerFactory") FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) { return new FlywayMigrationInitializer(flyway, null); } } 

This code requires Java 8, if you have Java 7 or earlier, replace (f)->{} with an inner class that implements FlywayMigrationStrategy

Of course, you can do this in XML just as easily.

Be sure to add this to your application.properties:

 flyway.baselineOnMigrate = true 
+17
source

Spring Automatically configuring Flyway downloads ensures that database migration is started before Hibernate initializes. In other words, you cannot rely on Flyway auto-tuning and use Flyway to populate the tables created by Hinernate.

One solution is to fully implement Flyway and use it to create tables and populate them. Then you can disable the creation of the Hibernate table ( spring.jpa.hibernate.ddl-auto=none ). This approach is more reliable, as it will allow your database to grow more easily. This is what I would recommend you do.

Another solution is to disable Flyway's automatic configuration ( flyway.enabled=false ) and configure it yourself. You can then configure Flyway to be hibernated so that Hibernate creates tables before Flyway tries to fill them.

+13
source

maybe due to order

Add to application.properties

 flyway.out-of-order = true 

or application.yml for spring

 flyway: out-of-order: true 
+1
source

I solved my problem. I deleted Flyway and just added data.sql

-1
source

All Articles