当前位置: 首页 > 知识库问答 >
问题:

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

段志
2023-03-14

当我使用JAP接口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)

共有1个答案

解沈义
2023-03-14

首先,您的代码应该读implements jpaspecificationexecutor

其次,实现JPaspecificationExecutor的全部要点是利用它提供的方法。您应该调用findAll(Specification,Sort)方法,而按cityid进行筛选应该是规范的一部分。

 类似资料:
  • 我试图在Spring Data JPA中创建存储库方法,如: 但它给我错误: 表格: 我想用create(即创建日期)按排序顺序查找表中的所有数据。 Spring 数据查询参数将根据其位置进行替换。但这可能会出错,绑定可能容易出错。因此,建议在方法参数中使用@Param注释来绑定查询参数名称。在查询中,您需要使用:p aramName来指示相同的参数名称将与方法参数绑定

  • 问题内容: 每次运行此php时,我都会不断收到相同的3个错误。我不知道我在做什么错,有人可以帮忙吗? 错误如下: [2014年5月5日19:20:50美国/芝加哥] PHP警告:mysqli_query()至少需要2个参数,第10行的/home/sagginev/public_html/Nutrifitness/search.php中提供了1个参数 [2014年5月5日19:20:50美国/芝加哥

  • 问题内容: 运行此代码时出现4个错误,这些错误将用户的电子邮件地址添加到名为的表中称为的数据库中。 $ con = new mysqli(’localhost’,’root’,’‘,’ecommerce’); 错误是这样的: 警告:mysqli_query()至少需要2个参数,第27行给出1个 第27行: $ query = mysqli_query(“ SELECT * FROM subscri

  • 问题内容: 我可以将单个 sort 参数传递给pyes中的搜索查询,如下所示: 但是我需要传递一个额外的参数来对得分相同的文档进行排序,如下所示: 我如何在pyes中做到这一点? 谢谢 问题答案: 如果您希望结果集中具有相同分数的结果按价格排序,请将价格附加到排序字符串: 默认情况下,排序顺序为升序。要传递排序顺序,请将 :asc 或 :desc 附加到sort参数

  • 我正在运行一个celery任务,它需要两个参数,如下所示: 和:

  • 在我的程序中,我主要想返回一个方法的结果,该方法测试两个学生之间的相等性(即,如果两个学生上的课的数量相等) 我创建了一个名为Student的类。然后我创建了两个函数并定义了它们的属性。实际上,我试图比较student类中的两个对象,而一个方法需要检查这两个对象之间的相等性。