Spring - 参数值与预期类型不匹配 [java.lang.Long]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32610834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring - Parameter value did not match expected type [java.lang.Long]
提问by Kurai Bankusu
I have the following line in my @Controller
to return results based on a string or partial string passed. The string may be numerical, in which case I parse it to a long value
我在我的以下行中@Controller
根据传递的字符串或部分字符串返回结果。字符串可能是数字,在这种情况下,我将其解析为长值
public @ResponseBody Page<Object> searchUsers(
@RequestParam(value = "search-string", required = false) String string) {
List<User> results = myRepo.findByUId(Long.valueOf(searchString).longValue();
//do stuff
}
My Repo Method
我的回购方法
@Query("SELECT u FROM User b WHERE u.uid LIKE :id%")
List<User> findByIdStartsWith(@Param("uid") Long uid);
However I get the following Exception (say my parameter is "1"):
但是我得到以下异常(假设我的参数是“1”):
java.lang.IllegalArgumentException: Parameter value [1%] did not match expected type [java.lang.Long (n/a)]
I don't think that I'm handling this parse correctly. Any ideas?
我认为我没有正确处理这个解析。有任何想法吗?
回答by Zbynek Vyskovsky - kvr000
Hibernate is trying casting the value "1%" to target type (User.id) which is Long. This is obviously impossible. LIKE works only with strings in hibernate, you may wrap with str(u.id) but see below.
Hibernate 正在尝试将值“1%”转换为 Long 的目标类型 (User.id)。这显然是不可能的。LIKE 仅适用于休眠中的字符串,您可以使用 str(u.id) 包装,但请参见下文。