java - Spring 启动 : Hibernate and Flyway boot order

标签 java hibernate spring-boot spring-data-jpa flyway

我已经创建了 Spring 应用程序。附上 Pom xml。

它有一个像这样的配置(如下)和一些 db/migration/V1__init.sql 用于 Flyway 数据库迁移工具。

它有 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>

<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>

最佳答案

我遇到了同样的问题。

我希望我的模式由 hibernate 创建,因为它与数据库无关。我已经解决了在我的 jpa 类中为我的应用程序找出一个好的模式的麻烦,我不喜欢重复自己。

但我希望以 flyway 擅长的版本化方式完成一些数据初始化。

Spring boot 在 hibernate 之前运行 flyway 迁移。为了改变它,我覆盖了 spring boot 初始化器什么也不做。然后我创建了第二个初始化程序,它在 hibernate 完成后运行。您需要做的就是添加这个配置类:

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 的内部类

当然,您可以在 xml 中轻松完成此操作。

确保将此添加到您的 application.properties:

flyway.baselineOnMigrate = true

关于java - Spring 启动 : Hibernate and Flyway boot order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37097876/

相关文章:

java - @Embedded 对象如果没有基本数据类型字段则不会自动实例化

hibernate - JPA 如何与 EntityManager.find() 一起处理部分、非不相交的继承(在每表类场景中使用 InheritanceType.JOINED)?

eclipse - Spring Tool Suite 创建新的spring starter 项目报错

java - Liquibase 和 Hibernate 从相同的属性文件读取

java - 如何修复 spring security oauth 中不安全的 jwt-set-uri?

java - ServletRegistrationBean 是否为每个注册的子 WebApplicationContext 使用不同的类加载器

java - JPA 唯一 boolean 列

java - 更有效的 KeyListener 实现

Java 将压缩的 .app 解压回 .app (OS X)

java - Hibernate GUID 本地生成器