Hibernate hql查询异常

时间:2017-12-14 14:59:52

标签: java mysql spring hibernate

我的@Query收到错误,如下所示:

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);

从我的控制器我调用方法:

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

但是当我执行get请求时,我的findByCityTypeAndName方法被触发,我收到以下错误:

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

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试在查询中使用concat

@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);

您也可以创建一个没有像这样的查询的方法

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