当前位置: 首页 > 知识库问答 >
问题:

Spring Boot 2.5.0、Spring Cloud 2020.0.2和Hibernate 5.4.31-H2数据库多行插入失败

柳修为
2023-03-14

在使用SB版本2.5.0的Spring Boot应用程序时,Spring Cloud(对于集中式配置2020.0.2)的Hibernate版本是5.4.31(我没有使用特定的Hibernate版本,这是根据Spring Boot兼容性)。使用H2数据库获取内存中的数据,因为我需要为演示创建示例应用程序。

在Resources文件夹中,我确实有我的SQL文件。当我将其命名为data.sql时,应用程序根本不会启动。当我将此文件重命名为import.sql时,我的应用程序启动了,但仍然面临多行插入的问题。

数据插入SQL文件

/* Data for Entity DataTable */
    INSERT INTO data_table (name) VALUES
    ('Data 1'),
    ('Data 2'),
    ('Data 3');

当我的应用程序使用Spring Boot 2.3.10运行时,它工作得非常好。释放和Spring Cloud Hoxton。SR5。

我也尝试了JPA和Hibernate的属性来解决这个问题,但仍然没有成功。查找我使用的属性如下:

spring.jpa.properties.hibernate.jdbc.batch_size=20
spring.jpa.properties.hibernate.order_inserts=true

由于更新了Spring Boot和Spring Cloud版本,我不确定是否有什么特别需要的东西来设置H2数据库以用于多行插入。

看起来与Hibernate或H2数据库问题相关。

WARN 7952 --- [           main] o.h.t.s.i.ExceptionHand lerLoggedImpl    : GenerationTarget encountered exception accepting command : Error executing DDL "('Data 2')" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "('Data 2')" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.31.Final.jar!/:5.4.31.Final]

...
...
...

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "('Data 2'[*]),"; expected "(, WITH, SELECT, TABLE, VALUES"; SQL statement: ('Data 2'), [42001-200]

更新

我已经将我的SQL文件名恢复为data.sql,然后我观察到同样的H2数据库异常即将到来,应用程序没有启动。

查找以下有关启动执行的详细信息:

WARN 4720 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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 [jar:file:/D:/Projects/SpringCloudDemoApp/spring-cloud-demo/target/demo-data-service-0.1.jar!/BOOT-INF/classes!/data.sql]: INSERT INTO data_table (name) VALUES ('Data 1'), ('Data 2'), ('Data 3'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "DATA_TABLE" not found; SQL statement: INSERT INTO data_table (name) VALUES ('Data 1'), ('Data 2'), ('Data 3') [42102-200]

...
...

ERROR 4720 --- [           main] 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 SQLscript statement #1 of URL [jar:file:/D:/Projects/SpringCloudDemoApp/spring-cloud-demo/target/demo-data-service-0.1.jar!/BOOT-INF/classes!/data.sql]: INSERT INTO data_table (name) VALUES ('Data 1'), ('Data 2'), ('Data 3'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "DATA_TABLE" not found; SQL statement: INSERT INTO data_table (name) VALUES ('Data 1'), ('Data 2'), ('Data 3') [42102-200]

另外,请查找我的实体类,以防出现问题,但不要认为实体类有任何问题,因为它可以与较旧的Spring Boot和Spring Cloud版本配合使用,但仍在此处共享。

DataTable实体类

@Entity
@Table(name = "data_table")
public class DataTable {   

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "dataid")
    private Long id;

    @Column(name = "name", nullable = false, unique = true, length = 200)
    private String name;

    // Getter Setters will follow here
}

应用程序YML文件

server:
    port: 9090
spring:
    datasource:
        url: jdbc:h2:mem:demodb
        driverClassName: org.h2.Driver
        username: scdemo
        password: scdemopass
    jpa:
        properties:
            hibernate:
                dialect: org.hibernate.dialect.H2Dialect
                jdbc:
                    batch_size: 20
                order_inserts: true
    h2:
        console:
            enabled: true
            path: /h2-console
            settings:
                trace: false

重要更新

我也尝试了同样的方法,只使用Spring Boot 2.5.0和JPA,使用H2数据库进行Hibernate,创建了一个只有4个文件的示例应用程序,Spring Boot应用程序文件要启动,实体类是数据。sql和应用程序。yml用于配置,并确保在相同条件下发生故障。我确认从数据加载数据时遇到了相同的错误。sql插入,这与旧的Spring Boot版本配合得很好。

我刚刚将SB版本从2.5.0升级到2.3.10。释放,则相同的代码起作用。

更多详细信息:

>

当<代码>数据时。sql重命名为导入。sql,Spring Boot版本2.5.0,以及导入中的Insert语句。sql文件是多行的,但希望将所有值保持在同一行上,这也很好。

data.sql重命名为import.sql,Spring Boot版本2.5.0import.sql文件中的插入语句是多行的并且在多行上时,此场景失败。因为它将每个新行上的语句视为DDL。在这种情况下,应用程序运行,但数据插入不发生。

GenerationTarget encountered exception accepting command : Error executing DDL "('Data 3')" via JDBC Statement

第四种情况是当<代码>数据时。带有Spring Boot版本2.5.0的sql和数据中的Insert语句。sql文件是多行的,位于多行上,此方案在数据源初始化时也会失败,应用程序根本不运行。

共享相同的示例代码:可以使用上述4个条件尝试源代码:demo-data-service

请提出建议。

共有2个答案

宗晟
2023-03-14

我在使用Spring Boot 2.5.0时发现自己无法在表名后添加换行符。使用Spring Boot 2.5.0,导入脚本应该命名为import.sql

所以,这引发了例外

INSERT into
 TRANSACTIONS (id, account_id, amount, currency, type) VALUES (5, 2, -10.00, 'GBP', 'MONEY_TRANSFER')

虽然这个没有

INSERT into TRANSACTIONS (id, account_id, amount, currency, type) VALUES (5, 2, -10.00, 'GBP', 'MONEY_TRANSFER')```
晏晨朗
2023-03-14

您需要将此添加到应用程序配置:

spring.jpa.defer-datasource-initialization=true

自Spring Boot 2.5.0以来,数据。默认情况下,sql在架构初始化之前执行。

另见:

带H2和数据的Spring Boot Data JPA。sql-未找到表

 类似资料:
  • 问题内容: 我正在尝试将JTable中的多行数据保存到数据库中,这是我的代码供参考: 问题是,它仅将一行数据插入数据库。有人可以帮帮我吗?:( 谢谢! 问题答案: 从循环中删除以下行代码并将其放置在循环之前 示例: 用以下代码替换您的代码 然后运行它认为它起作用。 对于批量插入示例,请参见https://my.vertica.com/docs/5.0/HTML/Master/14878.htm

  • 我正在inMemory数据库中插入数据,当插入数据时,我得到了一个问题, 使用boot、JPA、H2db在内存中插入数据的示例程序 > 创建Pojo并使用JPA注释进行注释 > 配置在app.prop:中 在data.sql文件中添加了给定表 为data.sql中提到的转换添加了名称。 在哪里配置;在Springboot中? 波乔 控制器 错误原因:对名为'in memorydatabaseShu

  • 问题内容: 从Actor中读取的文件:Zac Efron 传记:他出生于加利福尼亚州的圣路易斯·奥比斯波,并在阿罗约·格兰德附近长大。在几集《夏姆兰》(2004)的客串中,他以女孩疯狂的卡梅隆·贝尔的身份参加常规演出。埃夫隆还出演过许多飞行员,例如卡尔·兰克(Carl Laemke)的《大世界》(2003)(电视)和三重播放(2004)(TV)。 More_Bio:Efron于2006年6月毕业于

  • 利用INSERT语句将数据插入表中 数据插入 用来插入(添加)行到数据库。 插入完整的行 插入行的一部分 插入某些查询结果 插入完整的行 指定表名和被插入到新行中的值 编写依赖与特定列次序的SQL语句,这样做有时会出错,但编写方便。 mysql> INSERT INTO Customers -> VALUES('1000000006', -> 'Toy Land', ->

  • 问题内容: 我正在循环列表并将其插入数据库中,但它得到的更新记录一个接一个。最后我只在列表中的数据库最后一条记录中看到。 输入名称:Linux,Windows,Mac hibernate.cfg.xml: 这里有3次获得循环并插入数据库。但是以某种方式覆盖了这些值。因为我看到sql插入和更新在控制台中运行。 请帮助我将多个行插入数据库。 问题答案: Hibernate文档中有一章非常好的关于批处理