簡體   English   中英

Hibernate HQL查詢異常

[英]Hibernate hql query exception

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

任何想法如何解決這個問題?

嘗試在查詢中使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM