gpt4 book ai didi

java - 提供了至少 2 个参数,但按使用规范和排序查询时仅存在 1 个参数

转载 作者:行者123 更新时间:2023-12-02 10:06:18 25 4
gpt4 key购买 nike

当我使用 JAP 接口(interface) JpaSpecificationExecutor 时,我键入以下代码并抛出错误“至少提供了 2 个参数,但查询中仅存在 1 个参数”,我不知道为什么。

调用代码

@Override
public Result findSightsWithConditions(String cityId, Map<String, String> filter_map) {
//scoreOrder priceOrder minScore maxScore minPrice maxPrice
Specification<Sight> specification = new Specification<Sight>() {

@Override
public Predicate toPredicate(Root<Sight> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = Lists.newArrayList();
//predicates.add(criteriaBuilder.equal(root.get("cityId"), cityId));
if (null != filter_map.get("maxScore")) {
double max = Integer.parseInt(filter_map.get("maxScore"));
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("score"), max));
}
if (null != filter_map.get("maxPrice")) {
double max = Integer.parseInt(filter_map.get("maxPrice"));
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("price"), max));
}
if (null != filter_map.get("minScore")) {
double min = Double.parseDouble(filter_map.get("minScore"));
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("score"), min));
}

if (null != filter_map.get("minPrice")) {
double min = Double.parseDouble(filter_map.get("minPrice"));
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("price"), min));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
};
Sort sort = new Sort(Sort.Direction.DESC, "id");
String scoreOrder = filter_map.get("scoreOrder");
String priceOrder = filter_map.get("priceOrder");

if (scoreOrder != null) {
// 1 ASE 2DESC
sort = sort.and(new Sort(Integer.parseInt(scoreOrder) == 1 ? Sort.Direction.ASC : Sort.Direction.DESC, "score"));
}
if (priceOrder != null) {
sort = sort.and(new Sort(Integer.parseInt(scoreOrder) == 1 ? Sort.Direction.ASC : Sort.Direction.DESC, "price"));
}

List<Sight> sights = sightDao.findAllByCityId(cityId, specification, sort);
List<SightSimpleVO> sightSimpleVOS = sights.stream().map(sight -> {
SightSimpleVO vo = new SightSimpleVO();
BeanUtils.copyProperties(sight, vo, "pics", "labels");
vo.setPics(StringUtil.getList(sight.getPics(), ","));
vo.setLabels(StringUtil.getList(sight.getLabels(), ","));
return vo;
}).collect(Collectors.toList());
return Result.success().message("success!").withData(sightSimpleVOS);
}

存储库代码:

@Repository
public interface SightRepository extends PagingAndSortingRepository<Sight, Long>, JpaSpecificationExecutor {

List<Sight> findAllByCityId(String cityIds, Specification<Sight> specification, Sort var1);

}

我只想获得动态条件下的结果。异常信息如下:

java.lang.IllegalArgumentException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.
at org.springframework.util.Assert.isTrue(Assert.java:136)
at org.springframework.data.jpa.repository.query.QueryParameterSetterFactory$CriteriaQueryParameterSetterFactory.create(QueryParameterSetterFactory.java:290)
at org.springframework.data.jpa.repository.query.ParameterBinderFactory.lambda$createQueryParameterSetter$1(ParameterBinderFactory.java:139)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createQueryParameterSetter(ParameterBinderFactory.java:141)
at org.springframework.data.jpa.repository.query.ParameterBinderFactory.lambda$createSetters$0(ParameterBinderFactory.java:131)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createSetters(ParameterBinderFactory.java:132)
at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createSetters(ParameterBinderFactory.java:124)
at org.springframework.data.jpa.repository.query.ParameterBinderFactory.createCriteriaBinder(ParameterBinderFactory.java:75)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.getBinder(PartTreeJpaQuery.java:248)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.createQuery(PartTreeJpaQuery.java:164)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.doCreateQuery(PartTreeJpaQuery.java:92)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:210)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:129)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy159.findAllByCityId(Unknown Source)

最佳答案

首先,您的代码应为 implements JpaSpecificationExecutor<Sight>

其次,实现JPASpecificationExecutor的全部意义就是利用它提供的方法。您应该调用 findAll(Specification, Sort)方法,同时按 cityId 进行过滤应该是规范的一部分。

关于java - 提供了至少 2 个参数,但按使用规范和排序查询时仅存在 1 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317812/

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