简体   繁体   English

Spring Boot H2数据转换

[英]Spring Boot H2 data conversion

Spring Boot version: 2.1.2.RELEASE Spring Boot版本: 2.1.2.RELEASE
H2 version: 1.4.197 H2版本: 1.4.197
Query over JpaRepository: 通过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: 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 says: 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);

I get the same result if I try to execute this query directly from the H2 web console. 如果尝试直接从H2 Web控制台执行此查询,则会得到相同的结果。 If I got it right, the problem is in the statement: player0_.uid=(100 , 200) . 如果我player0_.uid=(100 , 200)正确,则问题出在以下语句中: player0_.uid=(100 , 200) The same query with the in player0_.uid in (100 , 200) executes fine from the web console. 从Web控制台可以很好地执行player0_.uid in (100 , 200) 中的 player0_.uid in (100 , 200)的相同查询。

Also I have the spring boot properties for H2: 我也有H2的spring boot属性:

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 如果“ uid”是Player中的字段,则不需要@Param
  • @Repository is also redundant @Repository也是多余的
  • 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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