gpt4 book ai didi

java.lang.IllegalArgumentException : Could not locate named parameter [user], 期待 [min, gender, max] 之一

转载 作者:行者123 更新时间:2023-12-05 07:10:27 33 4
gpt4 key购买 nike

我明白了

 INFO 4508 --- [nio-8081-exec-9]  j.r.q.QueryParameterSetter$ErrorHandling : Silently ignoring

java.lang.IllegalArgumentException: Could not locate named parameter [user], expecting one of [min, gender, max]
at org.hibernate.query.internal.ParameterMetadataImpl.getNamedParameterDescriptor(ParameterMetadataImpl.java:218) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.query.internal.ParameterMetadataImpl.getQueryParameter(ParameterMetadataImpl.java:187) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:188) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:493) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:106) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.lambda$setParameter$4(QueryParameterSetter.java:113) [spring-data-jpa-2.1.9.RELEASE.jar:2.1.9.RELEASE]
at org.springframework.data.jpa.repository.query.QueryParameterSetter$ErrorHandling$2.execute(QueryParameterSetter.java:185) ~[spring-data-jpa-2.1.9.RELEASE.jar:2.1.9.RELEASE]
at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter(QueryParameterSetter.java:113) [spring-data-jpa-2.1.9.RELEASE.jar:2.1.9.RELEASE]
at org.springframework.data.jpa.repository.query.ParameterBinder.lambda$bind$0(ParameterBinder.java:79) [spring-data-jpa-2.1.9.RELEASE.jar:2.1.9.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_231]
at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1082) ~[na:1.8.0_231]

代码

    @Repository
public interface UserRepo extends JpaRepository<User, Long> {
@Query("select new com.newcode.meeting.domain.dto.UserDto( " +
" us, " +
" SUM (case when ul.userLike = :user then 1 else 0 end) > 0 " +
") from User us left join us.likes ul where " +
" us.birthDate BETWEEN :max and :min " +
" and us.gender =:gender group by us ")
Page<UserDto> findUsersByBirthDateIsBetweenAndGenderDto(
Pageable pageable,
@Param("max") LocalDate max,
@Param("min") LocalDate min,
@Param("gender") Gender gender,
@Param("user") User user
);

如果您需要有人查看,请将 UserDto 分类。我不知道还有什么要添加的,这样它就不会写 -“看起来你的帖子主要是代码;请添加更多详细信息。”

 import com.fasterxml.jackson.annotation.JsonView;
import com.newcode.meeting.domain.Image;
import com.newcode.meeting.domain.ProfileDetail;
import com.newcode.meeting.domain.User;
import com.newcode.meeting.domain.Views;
import com.newcode.meeting.dto.Role;
import lombok.*;

import java.net.URL;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Set;

@Data
@EqualsAndHashCode(of = "id")
@AllArgsConstructor
@RequiredArgsConstructor
@Getter
@Setter
@ToString(of = {"id", "meLiked"})
@JsonView(Views.FullProfileDetail.class)
public class UserDto {
private Long id;
private String username;
private URL userpic;
private Gender gender;
private LocalDate birthDate;
private boolean isOnline;
private Set<Role> roles;
private boolean active;
private String region;
private String country;
private LocalDateTime lastVisit;
private ProfileDetail profileDetail;
@JsonView(Views.FullProfileDetailImage.class)
private Set<Image> images;
private boolean meLiked;
Integer likeNew;

public UserDto(User user, boolean meLiked) {
this.id = user.getId();
this.username = user.getUsername();
this.userpic = user.getUserpic();
this.gender = user.getGender();
this.birthDate = user.getBirthDate();
this.isOnline = user.isOnline();
this.roles = user.getRoles();
this.active = user.isActive();
this.region = user.getRegion();
this.country = user.getCountry();
this.lastVisit = user.getLastVisit();
this.profileDetail = user.getProfileDetail();
this.images = user.getImages();
this.meLiked = meLiked;
this.likeNew = user.getLikeNew();
}
}

public UserPageDto getAllUser(User user, String ageMax, String ageMin, String gender, Pageable pageable) {
user = userRepo.findUserById(user.getId());

LocalDate minDate = getDateForSearchUser(ageMin);
LocalDate maxDate = getDateForSearchUser(ageMax);
Gender sex;

if (gender.equals(Gender.FEMALE.toString())) {
sex = Gender.MALE;
} else {
sex = Gender.FEMALE;
}
Page<UserDto> page = userRepo.findUsersByBirthDateIsBetweenAndGenderDto(pageable, maxDate, minDate, sex, user);
return new UserPageDto(
page.getContent(),
page.getNumber(),
page.getTotalPages());
}

当我添加“Pageable”时,我开始收到此错误。但结果是得到了正确的。没有“Pageable”或参数“user”可以正常工作

问题是如何消除这个错误?

最佳答案

我遇到了类似的问题。

此错误与您的查询无关 - 它提示 countQuery,它计算所有可分页的结果,因此它可以计算它总共获得了多少页。

如果未明确声明,此 countQuery 将自动生成。所以它被生成为使用所有参数但不使用 User(这一代作为一种优化方式,可能会忽略 ORDER BY 或聚合,因为它只需要查询和订单匹配的记录数或其他数据无关紧要)。

因此生成的 countQuery 可能看起来像这样:

@Query("select count(us)" +
"from User us left join us.likes ul where " +
" us.birthDate BETWEEN :max and :min " +
" and us.gender =:gender group by us ")

如您所见 - 受影响的行数与原始查询中的行数相同,但不包括 :user,这就是 Hibernate 无法验证 countQuery 的原因。

将 Pageable 作为参数删除时,您不会遇到同样的错误,因为在这种情况下,不会生成 countQuery(在不使用 :user 参数时也是如此)。

您现在有三个选择:

  1. 使用 PageImpl 实现您自己的分页。
  2. 明确定义您的查询以使用 :user 参数:
@Query(value = "select new com.newcode.meeting.domain.dto.UserDto( " +
" us, " +
" SUM (case when ul.userLike = :user then 1 else 0 end) > 0 " +
") from User us left join us.likes ul where " +
" us.birthDate BETWEEN :max and :min " +
" and us.gender =:gender group by us ",
countQuery = "select count(us)" +
"from User us left join us.likes ul where " +
" us.birthDate BETWEEN :max and :min " +
" and us.gender =:gender group by us " +
"order by :user)
  1. 首先检索 UserEntities,然后在将其映射到 DTO 时 - 将此额外计算在内列(通过单独的 JPA 查询或通过实体中的相关集合使用Java 方法)。

关于java.lang.IllegalArgumentException : Could not locate named parameter [user], 期待 [min, gender, max] 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61231447/

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