简体   繁体   English

如何将谓词列表添加到 CriteriaBuilder.or

[英]How to add a list of Predicates to CriteriaBuilder.or

I have a List to append in an or condition我有一个要附加在 or 条件中的列表

The Issue I am facing is when I am iterating over the List and adding it to the CategoryBuilder then it takes the last Predicate我面临的问题是,当我遍历 List 并将其添加到 CategoryBuilder 时,它需要最后一个 Predicate

Following is the example:以下是示例:

public static Specification<Billoflading> hasTenantAndBillingCodeCombination(List<WhoOwnsIt> list,
            Date formattedFromDate, Date formattedToDate, String carrierFilter, String supplierFilter,
            String terminalFilter) {
        return (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            for (WhoOwnsIt whoOwnsIt : list) {
                String containsLikePatternForTenant = getContainsLikePattern(whoOwnsIt.getWholesalerTenantID(), true);

                Predicate pred = cb.and(cb.like(cb.lower(root.<String>get("tenant")), containsLikePatternForTenant),
                        cb.equal(cb.lower(root.<String>get("billingCode")), whoOwnsIt.getBillingCode()),
                        cb.greaterThanOrEqualTo(root.<Date>get("scheduleDate"), formattedFromDate),
                        cb.lessThanOrEqualTo(root.<Date>get("scheduleDate"), formattedToDate));

                Predicate predWithTenant = null;

                if (null != carrierFilter) {
                    predWithTenant = cb.and(cb.equal(cb.lower(root.<String>get("tenant")), carrierFilter));
                }

                if (null != predWithTenant)
                    predicates.add(predWithTenant);
                else
                    predicates.add(pred);

            }
            Predicate finalPredicate;
            // This commented section below needs to be replaced with a 
            //better solution
            //for(Predicate predicate : predicates){
            //  cb.or(predicate);
            //}

            return finalPredicate;
        };
    }

I also tried making the List an array for passing it to the finalPredicate.or as follows我还尝试使 List 成为一个数组以将其传递给 finalPredicate.or 如下

Predicate finalQuery = cb.or(predicates.toArray());

But this code gave compilation error as the parameters needed is varargs ie但是这段代码给出了编译错误,因为所需的参数是可变参数,即

or method accepts (Predicate... predicates) as parameter list or 方法接受 (Predicate... predicates) 作为参数列表

Can you please provide some simple solution to this issue?您能否为这个问题提供一些简单的解决方案?

尝试这个:

Predicate finalQuery = cb.or(predicates.toArray(new Predicate[0]));

作为补充,我建议对较新的 Java 版本使用以下表示法:

builder.or(predicates.toArray(Predicate[]::new))

A sample code I wrote that might be helpful, here am gathering all predicates in a list and then converted list to array which is getting consumed by builder.我编写的一个示例代码可能会有所帮助,这里将所有谓词收集在一个列表中,然后将列表转换为由构建器使用的数组。

 public List< EntityClass> getData(List<Long> ids) {

    List<List<Long>> idPartitions = ListUtils.partition(ids, 999);
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntityClass> criteria = builder.createQuery(EntityClass.class);

    Root<EntityClass> root = criteria.from(EntityClass.class);

    List<Predicate> predicates = new ArrayList<>(idPartitions.size());
    for (List<Long> idPartition : idPartitions) {
      predicates.add(root.get("id").in(idPartition));
    }

    criteria.where(builder.and(predicates.toArray(new Predicate[0])));
    List<EntityClass> objects = entityManager.createQuery(criteria).getResultList();
    return objects;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM