Reputation: 4591
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
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"):
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?
Upvotes: 5
Views: 22656
Reputation: 18865
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.
Upvotes: 6