【问题标题】:H2 database integer data conversion errorH2数据库整数数据转换错误
【发布时间】:2021-08-13 12:15:54
【问题描述】:

这是我的模型类:

package com.arizonainc.dev.eLibrarySystem.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Book {
@Id
@GeneratedValue
private long id;

private String title;
private String genre;
private String author;
@Column(nullable = false)
private int availableQuantity;

protected Book() {
    
}

public Book(String title, String genre, String author, int availableQuantity) {
    super();
    this.title = title;
    this.genre = genre;
    this.author = author;
    this.availableQuantity = availableQuantity;
}

这是我在 src/main/resources 中的 data.sql 文件:

insert into BOOK values (10001, '12 Rules For Life', 'Non-fiction', 'Jordan Peterson', 5);
insert into BOOK values (10002, 'Maps of Meaning', 'Non-fiction', 'Jordan Peterson', 4);
insert into BOOK values (10003, 'Crime and punishment', 'Fiction', 'Fyodor Dostoevsky', 3);
insert into BOOK values (10004, 'Peace and War', 'Fiction', 'Leo Tolstoy', 3);

这是控制台中的错误消息:

Data conversion error converting "'Non-fiction' (BOOK: ""AVAILABLE_QUANTITY"" INTEGER NOT NULL)"; SQL statement: insert into BOOK values (10001, '12 Rules For Life', 'Non-fiction', 'Jordan Peterson', '5') [22018-200]

我做错了什么,我该如何解决?

【问题讨论】:

    标签: spring spring-boot spring-data-jpa h2


    【解决方案1】:

    请在 INSERT 语句中明确指定字段的顺序:

    insert into BOOK(id, title, genre, author, availableQuantity) values (10001, '12 Rules For Life', 'Non-fiction', 'Jordan Peterson', 5);
    insert into BOOK(id, title, genre, author, availableQuantity) values (10002, 'Maps of Meaning', 'Non-fiction', 'Jordan Peterson', 4);
    insert into BOOK(id, title, genre, author, availableQuantity) values (10003, 'Crime and punishment', 'Fiction', 'Fyodor Dostoevsky', 3);
    insert into BOOK(id, title, genre, author, availableQuantity) values (10004, 'Peace and War', 'Fiction', 'Leo Tolstoy', 3);
    

    这应该可以解决问题。

    如果您没有在 INSERT 语句中明确指定字段的顺序,那么顺序可能与您期望的不同。

    在您的示例中,出于某种原因,H2 INSERT 语句需要 3rd 位置上的“availableQuantity”字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 2020-09-14
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2013-04-15
      相关资源
      最近更新 更多