简体   繁体   中英

Hibernate hql query exception

I'm getting an error from my @Query, which is the following:

UserRepository.java

@Query(value = "select u from User u\n" +
          "  where u.city = :city and u.dentistType = :type\n" +
          "  and (u.firstName like ':name%' or u.lastName like ':name%')")
  List<User> findByCityTypeAndName(@Param("city") String city, @Param("type") DentistType type, @Param("name") String name);

From my controller I call the method:

List<User> result = userRepository.findByCityTypeAndName(city, DentistType.valueOf(type), name);

But when I perform a get request, thus my findByCityTypeAndName method is fired, I get the following error:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that name [name] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [name] did not exist] with root cause

java.lang.IllegalArgumentException: Parameter with that name [name] did not exist

Any idea how to fix this?

Try using concat in your query

@Query(value = "select u from User u " +
          "  where u.city = :city and u.dentistType = :type " +
          "  and (u.firstName like CONCAT(:name,'%') or u.lastName like CONCAT(:name,'%')")
  List<User> findByCityTypeAndName(@Param("city") String city, @Param("type") DentistType type, @Param("name") String name);

You can also create a method without a query something like this

List<User> findByCityAndDentistTypeAndFirstNameStartingWithOrLastNameStartingWith(String city, DentistType type, String firstName, String lastName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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