Spring Boot 版本:2.1.2.RELEASEH2 版本:1.4.197通过 JpaRepository 查询:@Repositorypublic interface PlayerStorage extends JpaRepository<Player, Long> { List<Player> findByUid(@Param("uid") List<Integer> uid);}...List<Player> foundByUids = playerStorage.findByUid(Arrays.asList(100, 200));Spring 生成并执行查询: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 = 22018The 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). 与in 相同的查询player0_.uid in (100 , 200)可以从 Web 控制台正常执行。我也有 H2 的弹簧靴属性:spring.datasource.driver-class-name=org.h2.Driverspring.jpa.database=H2spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
2 回答
眼眸繁星
TA贡献1873条经验 获得超9个赞
错误可能在存储库代码中:
public interface PlayerStorage extends JpaRepository<Player, Long> {
List<Player> findByUid(@Param("uid") List<Integer> uid);
}
从文档中它应该是:
public interface PlayerStorage extends JpaRepository<Player, Long> {
List<Player> findByUidIn(@Param("uid") List<Integer> uid);
}
还:
如果“uid”是 Player 中的字段,则不需要 @Param
@Repository 也是多余的
我建议添加一些验证,以确保传递给方法的列表不为空,否则会导致异常。
添加回答
举报
0/150
提交
取消