gpt4 book ai didi

java - Hibernate hql查询异常

转载 作者:行者123 更新时间:2023-11-30 21:49:39 25 4
gpt4 key购买 nike

我的@Query 出现错误,错误如下:

用户库.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);

我从我的 Controller 调用方法:

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

关于java - Hibernate hql查询异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47816363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com