简体   繁体   中英

Spring Boot H2 data conversion

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

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.

尝试将您的方法重命名为

List<Player> findByUidIn(@Param("uids") List<Integer> uids);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM