2

I have problem with h2 database.

I try to into values to the database like this

INSERT INTO INGREDIENT(id,name,type) values ('FLTO','pszenna','WRAP');

But then i got this error

Data conversion error converting "'WRAP' (INGREDIENT: ""TYPE"" INTEGER)"; SQL statement:

also when i do smth like down my query is working

INSERT INTO INGREDIENT(id,name,type) values ('FLTO','pszenna','3');

but in schema.sql file the type field is varchar so why this doesnt working

create table if not exists Ingredient (
  id varchar(4) not null,
  name varchar(40) not null,
  type varchar(10) not null
);
5
  • 1
    H2 believes your type column is an integer, and it complains that it can't convert 'WRAP' to integer. The second example works beacause '3' can be converted to 3. Commented Mar 12, 2021 at 23:54
  • Okey i know this but i create a field type like varchar not int so this should working Commented Mar 12, 2021 at 23:56
  • Verify that create and insert are applied to the same database; append ;IFEXISTS=TRUE, as shown here to avoid creating spurious database files. Commented Mar 13, 2021 at 0:00
  • Try to drop and re-create the table. You may have an older schema still, and "create table if exists" ends up doing nothing. Commented Mar 13, 2021 at 0:17
  • @Spectra how did you solve this? Commented Oct 4, 2022 at 2:02

3 Answers 3

2

The Type type field of Ingredient entity class should be annotated with @Enumerated(EnumType.STRING)

    @Enumerated(EnumType.STRING)
    private final Type type;
Sign up to request clarification or add additional context in comments.

Comments

0

I tried to reproduce your error with code (using Spring's JdbcTemplate) but it runs succesfully:

@Test
public void testH2() {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setUrl("jdbc:h2:file:/tmp/test123");
    JdbcTemplate jdbc = new JdbcTemplate(ds);
    jdbc.update("drop table if exists ingredient");
    jdbc.update(
            "create table if not exists ingredient(" +
            " id varchar(4) not null," +
            " name varchar(40) not null," +
            " type varchar(10) not null)"); // if this is " type int)" I get the same error message
    jdbc.update("delete from ingredient");
    showTable(jdbc, "empty");
    jdbc.update("insert into ingredient(id,name,type) values ('FLTO','pszenna','3')");
    showTable(jdbc, "added FLTO");
    jdbc.update("insert into ingredient(id,name,type) values ('FLTW','pszennb','WRAP')");
    showTable(jdbc, "added FLTW");
}

void showTable(JdbcTemplate jdbc, String message) {
    System.out.println("=== " + message + " ===");
    jdbc.query("select id, name, type from ingredient order by id", rs -> {
        int n = 0;
        while (rs.next()) {
            System.out.println("id=" + rs.getString(1) + " name=" + rs.getString(2) + " type=" + rs.getString(3));
            n++;
        }
        System.out.println(n + " rows");
        return n;
    });
}

If I change the type of type to int I get the exact same error message as you.

Comments

0

Okey, so i think the problem is smth with the database because when i want

DROP TABLE Ingredient;

I got error table "Ingrdient" not found.

This is my application.properties file

# Enabling H2 Console
spring.h2.console.enabled=true

# Custom H2 Console URL
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:testdb

Idk what i should to do

2 Comments

H2 in-memory databases are a little tricky because they disappear when you close the last connection. If you change your url to something like spring.datasource.url=jdbc:h2:file:mydb you might get better results.
Another way to get around it is to add ;DB_CLOSE_DELAY=-1 to the jdbc url like this: spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.