Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 2.5.0升级后,data.sql脚本出现Spring Boot数据源初始化错误_Spring Boot_Hibernate_Spring Data Jpa - Fatal编程技术网

Spring boot 2.5.0升级后,data.sql脚本出现Spring Boot数据源初始化错误

Spring boot 2.5.0升级后,data.sql脚本出现Spring Boot数据源初始化错误,spring-boot,hibernate,spring-data-jpa,Spring Boot,Hibernate,Spring Data Jpa,我有一个配置了SpringBoot和SpringDataJPA的项目 我的项目包含以下data.sql初始化脚本我刚开始开发我的应用程序,这就是我使用嵌入式H2数据库的原因: INSERT INTO Project(id, name) VALUES (1, 'Project 1'); 我定义了以下实体: @Entity public class Project { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

我有一个配置了SpringBoot和SpringDataJPA的项目

我的项目包含以下data.sql初始化脚本我刚开始开发我的应用程序,这就是我使用嵌入式H2数据库的原因:

INSERT INTO Project(id, name) VALUES (1, 'Project 1');
我定义了以下实体:

@Entity
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    protected Project() {
    }

    public Project(String name) {
        this.name = name;
    }
    // ...
}
我没有任何其他配置/设置

ERROR 5520 ---[  restartedMain] o.s.boot.SpringApplication               :Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/path/project/target/classes/data.sql]: INSERT INTO Project(id, name) VALUES (1, 'Project 1'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PROJECT" not found; SQL statement:
INSERT INTO Project(id, name) VALUES (1, 'Project 1') [42102-200]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
...
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/path/project/target/classes/data.sql]: INSERT INTO Project(id, name) VALUES (1, 'Project 1'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PROJECT" not found; SQL statement:
INSERT INTO Project(id, name) VALUES (1, 'Project 1') [42102-200]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:622)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254)
...
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PROJECT" not found; SQL statement:
INSERT INTO Project(id, name) VALUES (1, 'Project 1') [42102-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
当然,在升级之前,应用程序启动得很好;博士

SpringBoot似乎修改了它使用SQL脚本在2.5.0中初始化数据源的方式

可以通过在项目中包含以下属性来解决此问题:

spring:
  jpa:
    defer-datasource-initialization: true
解释如下:

在2.5.0中引入的更改中,现在似乎data.sql脚本是在Hibernate初始化之前执行的:

由于我依赖ORM机制(即Hibernate)从实体定义创建模式,所以在执行初始化SQL脚本时,数据库表不存在;博士

SpringBoot似乎修改了它使用SQL脚本在2.5.0中初始化数据源的方式

可以通过在项目中包含以下属性来解决此问题:

spring:
  jpa:
    defer-datasource-initialization: true
解释如下:

在2.5.0中引入的更改中,现在似乎data.sql脚本是在Hibernate初始化之前执行的:

由于我依赖ORM机制(即Hibernate)从实体定义创建模式,所以在执行初始化SQL脚本时,数据库表并不存在