Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Parameter value did not match expected type [java.lang.Long]

Tags:

java

spring

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?

like image 995
Clay Banks Avatar asked Sep 16 '15 14:09

Clay Banks


1 Answers

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.

like image 198
Zbynek Vyskovsky - kvr000 Avatar answered Oct 27 '22 17:10

Zbynek Vyskovsky - kvr000