Skip to content
Advertisement

How to fix data conversion error: jdbcTemplate.update, writing Enum to H2 database

I use PostgreSql for my main code and H2 for testing, and get different results (test fails). My class with an Enum type field

JavaScript

SQL schema is

JavaScript

My DAO class

JavaScript

In my test method:

JavaScript

Everything works with PostgreSql, but test with H2 fails with data conversion error:

JavaScript

I think “java.sql.Types.OTHER” that I used in PreparedStatement for Gender is converted automatically by Postgres, but not by H2 engine. http://www.h2database.com/html/datatypes.html#enum_type says that Enum is mapped to Integer. Is this the problem? Is there a workaround?

Advertisement

Answer

H2 database engine doesn’t support Enum values as it is implemented in Postgre. I solved my problem using a workaround:

  1. Removed Enum type declaration from the SQL schema
  2. Replaced Enum field in SQL schema by Varchar type (but not in Java model class)
  3. Modified Dao layer using .toString() in update and create methods

Now tests run correctly in both databases.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement