Spring Boot:Hibernate 和 Flyway 引导顺序 [英] Spring Boot: Hibernate and Flyway boot order
问题描述
我已经创建了 Spring 应用程序.附上pom xml.
I have created Spring application. Pom xml is attached.
它有这样的配置(如下)和一些用于 Flyway 数据库迁移工具的 db/migration/V1__init.sql.
It has a config like this (below) and some db/migration/V1__init.sql for Flyway db migration tool.
它有 hsqldb 内存数据库,它是在应用程序启动后创建的.创建后很干净.
It has hsqldb in-memory database and it is created after application is started. It is clean after creation.
我希望 Hibernate 创建基于实体类的模式,然后 Flyway 填充表.现在 Flyway 在创建表之前启动 V1__init.sql 并抛出异常.如何更改此订单或我可以采取什么解决方案?
I want Hibernate to create a schema based on entity classes and then Flyway fills the tables. Now Flyway starts V1__init.sql before tables is created and throw an exception. How can I change this order or what solution can I do?
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>
推荐答案
我遇到了同样的问题.
我希望我的架构由休眠创建,因为它与数据库无关.我已经在 jpa 类中为我的应用程序找出了一个很好的架构,我不喜欢重复自己.
I wanted my schema to be created by hibernate because of it's database independence. I already went through the trouble of figuring out a nice schema for my application in my jpa classes, I don't like repeating myself.
但我希望以 flyway 擅长的版本化方式完成一些数据初始化.
But I want some data initialization to be done in a versioned manner which flyway is good at.
Spring Boot 在休眠之前运行 Flyway 迁移.为了改变它,我覆盖了 spring boot 初始化器什么都不做.然后我创建了第二个初始化程序,它在休眠完成后运行.你需要做的就是添加这个配置类:
Spring boot runs flyway migrations before hibernate. To change it I overrode the spring boot initializer to do nothing. Then I created a second initializer that runs after hibernate is done. All you need 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);
}
}
该代码需要 java 8,如果您有 java 7 或更早版本,请将 (f)->{}
替换为实现 FlywayMigrationStrategy
That code needs java 8, If you have java 7 or earlier, replace (f)->{}
with an inner class that implements FlywayMigrationStrategy
当然,您可以同样轻松地在 xml 中执行此操作.
Of course you can do this in xml just as easily.
确保将此添加到您的 application.properties:
Make sure to add this to your application.properties:
flyway.baselineOnMigrate = true
这篇关于Spring Boot:Hibernate 和 Flyway 引导顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!