Reputation: 367
This is my model 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;
}
This is my data.sql file in src/main/resources:
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);
This is the error message in the console:
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]
What am I doing wrong and how do I fix this?
Upvotes: 1
Views: 731
Reputation: 1367
Please, specify explicitly the sequence of fields in the INSERT statement:
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);
This should solve the issue.
If you don't specify the sequence of fields explicitly in the INSERT statement, then the order might not be, as you expect it.
In your example, H2 INSERT statement expects "availableQuantity" field on the 3rd position, for some reason.
Upvotes: 2