【问题标题】:Hibernate hql query exceptionHibernate hql 查询异常
【发布时间】:2017-12-14 14:59:51
【问题描述】:

我的@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

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

【问题讨论】:

  • ':name%' 是不可能的。在调用findByCityTypeAndName 方法之前,您需要自己删除引号并附加%
  • 谢谢,这解决了我的问题。

标签: java mysql spring hibernate


【解决方案1】:

尝试在查询中使用 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2011-05-18
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多