简体   繁体   English

data.sql 文件未将数据泵入 spring 引导中的表

[英]data.sql file not pumping data to tables in spring boot

If I use the spring-boot-starter-parent version 2.5.0 data.sql file doesn't get executed.如果我使用 spring-boot-starter-parent 版本 2.5.0 data.sql 文件不会被执行。 But if I use version 2.4.1 everything works perfectly.但如果我使用 2.4.1 版本,一切都会完美运行。 I'm putting my codes below as well.我也把我的代码放在下面。

Spring Application Runner Class: Spring 应用程序运行器 Class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

My Entity Class:我的实体 Class:

package com.example.demo.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@NoArgsConstructor
@AllArgsConstructor

@Getter
@Setter
@Entity
@Table(name = "cars")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer id;

    @Column(name = "car_name")
    public String carName;

    @Column(name = "car_year")
    public Integer carYear;

}

My application.properties File:我的 application.properties 文件:

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always

My data.sql File:我的数据.sql 文件:

INSERT INTO cars(car_name, car_year)
VALUES('car1', 2001);

INSERT INTO cars(car_name, car_year)
VALUES('car2', 2002);

INSERT INTO cars(car_name, car_year)
VALUES('car3', 2003);

INSERT INTO cars(car_name, car_year)
VALUES('car4', 2004);

My pom.xml File:我的 pom.xml 文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Some extra information with pictures:一些带图片的额外信息:

My project structure and the table created in the database我的项目结构和数据库中创建的表

And also I've never seen these two lines while I was running the application on spring-boot-starter-parent version 2.4.1而且我在 spring-boot-starter-parent 版本 2.4.1 上运行应用程序时从未见过这两行

I'm completely ok with just using spring-boot-starter-parent version 2.4.1 which I was using until now, but I'm also so curious about the reason for this issue.我完全可以只使用我一直使用的 spring-boot-starter-parent 2.4.1 版,但我也很好奇这个问题的原因。

The Spring Boot 2.5 Release Notes says to set Spring Boot 2.5 发行说明说要设置

spring.jpa.defer-datasource-initialization: true

to allow Hibernate to create the schema before loading data.sql.允许 Hibernate 在加载 data.sql 之前创建模式。

I can see they have mentioned it here :我可以看到他们在这里提到过:

By default, data.sql scripts are now run before Hibernate is initialized.默认情况下,data.sql 脚本现在在 Hibernate 初始化之前运行。 This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase.这使基于脚本的基本初始化行为与 Flyway 和 Liquibase 的行为保持一致。 If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it's populated via data.sql.虽然不建议混合数据库初始化技术,但这也将允许您使用 schema.sql 脚本在通过 data.sql 填充之前构建 Hibernate 创建的模式。

So you can try所以你可以试试

spring.jpa.defer-datasource-initialization: true

A worth read 9.3.值得一读9.3。 Initialize a Database Using Basic SQL Scripts 使用基本 SQL 脚本初始化数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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