春季启动H2的数据转换

问题描述 投票:0回答:2

春天开机版本:2.1.2.RELEASE H2版本:1.4.197 查询过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));

春天生成并执行查询:

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说:

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);

我得到同样的结果,如果我尝试直接从H2 Web控制台执行这个查询。如果我这样做是正确的,问题是在声明中:player0_.uid=(100 , 200)。与在player0_.uid in (100 , 200)相同的查询执行从Web控制台罚款。

此外,我对H2的春天引导属性:

spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.database=H2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
java spring-data-jpa h2
2个回答
3
投票

也许,错误是在仓库代码:

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

documentation它应该是:

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

也:

  • 如果“UID”是在播放器领域,然后@Param不需要
  • @Repository也是多余的
  • 我会建议增加一些验证传递给方法列表不为空,否则会导致异常。

2
投票

尝试你的方法重新命名为

List<Player> findByUidIn(@Param("uids") List<Integer> uids);
热门问题
推荐问题
最新问题