简体   繁体   中英

Problem with Spring JPA data.sql script in Gradle only

I have a project which I can run with Maven but not with Gradle. The project consists of one very simple entity and data.sql file where a table for this entity gets populated with initial data. When I try to run this project with Maven - everything is ok. But when I try to run the same code but as a Gradle project I am getting an error, saying that insert in data.sql can not be done as the table for the entity does not exist. If I remove data.sql and run project one more time - table is created. After the table is created I can run the project one more time with data.sql and it will populate the table. So it seems like Maven project runs data.sql after entity tables are created and in Gradle it happens other way around. Why so? Maybe I wrongly assume that my Maven and Gradle configurations are the same and there are some subtle difference? Thanks a lot for your answers in advance.

Maven pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.iamvickyav.springboot</groupId>
    <artifactId>SpringBootRestWithH2</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Gradle build file:

plugins {
    id("java")
    id("org.springframework.boot") version "2.5.0"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
}

group = "com.github.stanislavmikheyev"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("com.h2database:h2")
}

You have one major difference between your 2 configuration. With maven you use Spring boot version 2.2.4, with gradle 2.5.0

Since 2.5.0 i believe there was a change in the order of database operation when starting a Spring boot app.

You can either fix your version with 2.2.4 ans change nothing. Or set the property spring.jpa.defer-datasource-initialization=true and it should work in 2.5

Ref link: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM