Spring Boot H2 data conversion

IgorZ

Spring Boot version: 2.1.2.RELEASE
H2 version: 1.4.197
Query over JpaRepository:

@Repository
public interface PlayerStorage extends JpaRepository<Player, Long> {
  List<Player> findByUid(@Param("uid") List<Integer> uid);
}
...
List<Player> foundByUids = playerStorage.findByUid(Arrays.asList(100, 200));

Spring generates and executes a query:

Data conversion error converting "(100, 200)"; SQL statement:
select
        player0_.id as id1_3_,
        player0_.uid as uid3_3_ 
    from
        player player0_ 
    where
        player0_.uid=(100 , 200)
[22018-197]
...
Caused by: java.lang.NumberFormatException: For input string: "(100, 200)"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at org.h2.value.Value.convertTo(Value.java:1061)

H2 says:

DATA_CONVERSION_ERROR_1 = 22018
The error with code 22018 is thrown when trying to convert a value to a data type where the conversion is undefined, or when an error occurred trying to convert.
Example:
CALL CAST(DATE '2001-01-01' AS BOOLEAN);
CALL CAST('CHF 99.95' AS INT);

I get the same result if I try to execute this query directly from the H2 web console. If I got it right, the problem is in the statement: player0_.uid=(100 , 200). The same query with the in player0_.uid in (100 , 200) executes fine from the web console.

Also I have the spring boot properties for H2:

spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.database=H2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Oleksii Zghurskyi

Probably, the error is in repository code:

public interface PlayerStorage extends JpaRepository<Player, Long> {
  List<Player> findByUid(@Param("uid") List<Integer> uid);
}

From documentation it should be:

public interface PlayerStorage extends JpaRepository<Player, Long> {
  List<Player> findByUidIn(@Param("uid") List<Integer> uid);
}

Also:

  • if "uid" is the field in Player, then @Param is not required
  • @Repository is also redundant
  • I would suggest to add some validation that list passed to the method is not empty, otherwise it will result in exception.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Store pictures in H2 database spring boot thymleaf

Unknown data type when using an integer over NamedParameterJDBCTemplate on H2 [SPRING-BOOT]

How to enable H2 Database Server Mode in Spring Boot

Spring Boot in memory database H2 doesn't load data from file on initialization

File based h2 persisted but not loaded in Spring Boot

Spring Boot application with H2 file database

spring boot default H2 jdbc connection (and H2 console)

OOM in Docker with Spring Boot + H2 + JPA and Batch Processing

Switching from H2 to PostgreSQL with spring boot

How to load initial data in H2 when using flyway on spring boot application?

H2 Database created with spring-boot-starter-data-jpa but not with spring-boot-starter-data-jdbc

Create foreign key in Spring boot - H2 database

Foreign key problem in Spring Boot - h2 database

How to load h2 data in spring boot test before component initialization

Executing H2 under Spring Boot

Spring Boot: H2 database not saved to file

Loading initial test data in H2 in spring boot

Spring boot with H2 and Oracle

Spring Boot 2.0, H2, Unable to start app on existing H2 DB file

Tests in Spring Boot with database H2

Spring Boot 2.2.1 H2 Failures?

Data Conversion Error H2 Database

Get data from h2 database via spring-boot [JAVA]

Insert data in H2 database with Spring Boot JPA on post

Spring Boot with H2 is running data.sql every time the web app starts, is this normal?

Spring Boot h2 initial data not creating table first

Liquibase migration with H2 and Spring boot

Spring Boot CRUD REST API + Spring Data JPA + H2 DB: Getting NPE on tests but not on Postman

Spring Boot Two Datasource configuration for H2 DB failed to create tables and insert data