Spring Boot:Hibernate和Flyway引导顺序 [英] Spring Boot: Hibernate and Flyway boot order
问题描述
我创建了Spring应用程序。
它有这样的配置(下面)和一些用于Flyway db迁移工具的db / migration / V1__init.sql。
它具有hsqldb内存数据库,它在应用程序启动后创建。它在创建后是干净的。
我希望Hibernate根据实体类创建模式,然后Flyway填充表格。现在Flyway在创建表之前启动V1__init.sql并引发异常。如何更改此订单或我可以做什么解决方案?
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>
<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< java.version> 1.8< /java.version>
< / properties>
<依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< version> 1.3.2.RELEASE< / version>
< /依赖关系>
< dependency>
< groupId> org.hsqldb< / groupId>
< artifactId> hsqldb< / artifactId>
< scope>运行时< / scope>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.3.11.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< version> 4.2.5.RELEASE< / version>
< /依赖关系>
< dependency>
< groupId> org.thymeleaf< / groupId>
< artifactId> thymeleaf-spring4< / artifactId>
< version> 2.1.4.RELEASE< / version>
< /依赖关系>
<! - 在Thymeleaf中使用'LEGACYHTML5'模式 - >
< dependency>
< groupId> net.sourceforge.nekohtml< / groupId>
< artifactId> nekohtml< / artifactId>
< version> 1.9.21< / version>
< /依赖关系>
< dependency>
< groupId> xml-apis< / groupId>
< artifactId> xml-apis< / artifactId>
< version> 1.4.01< / version>
< /依赖关系>
< dependency>
< groupId> org.flywaydb< / groupId>
< artifactId> flyway-core< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
< version> 1.3.3.RELEASE< / version>
< /依赖关系>
< /依赖关系>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-maven-plugin< / artifactId>
< / plugin>
< / plugins>
< / build>
我有同样的问题。
我希望我的模式由hibernate创建,因为它的数据库独立性。我已经经历了为我的jpa类中的应用程序计算出一个很好的模式的麻烦,我不喜欢重复自己。
但我想要一些数据初始化以飞路擅长的版本方式完成。
Spring在hibernate之前运行flyway迁移。为了改变它,我覆盖了spring引导初始化器什么也不做。然后我创建了第二个初始化器,在休眠完成后运行。所有你需要做的就是添加这个配置类:
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 {
$ b $ / **
*忽略默认的flyway初始值设定项不做任何操作
* /
@Bean
FlywayMigrationInitializer flywayInitializer(Flyway flyway){
返回新的FlywayMigrationInitializer(flyway,(f) - > {});
}
/ **
*在jpa创建模式
* /
@Bean后创建第二个飞桥初始化器
@DependsOn(entityManagerFactory)
FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway){
返回新的FlywayMigrationInitializer(flyway,null);
}
}
该代码需要java 8,如果你有java 7或更早的版本,用一个实现(f) - > {} 。 spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/flyway/FlywayMigrationStrategy.htmlrel =noreferrer> FlywayMigrationStrategy
当然,你也可以在xml中轻松做到这一点。
确保将其添加到application.properties中:
flyway.baselineOnMigrate = true
I have created Spring application. Pom xml is attached.
It has a config like this (below) and some db/migration/V1__init.sql for Flyway db migration tool.
It has hsqldb in-memory database and it is created after application is started. It is clean after creation.
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>
I had the same issue.
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.
But I want some data initialization to be done in a versioned manner which flyway is good at.
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);
}
}
That code needs 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.
Make sure to add this to your application.properties:
flyway.baselineOnMigrate = true
这篇关于Spring Boot:Hibernate和Flyway引导顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!