H2数据库整型数据转换错误
H2 database integer data conversion error
这是我的模型class:
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]
我做错了什么,我该如何解决?
请明确指定 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”字段位置,出于某种原因。
这是我的模型class:
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]
我做错了什么,我该如何解决?
请明确指定 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”字段位置,出于某种原因。