I have a List to append in an or condition
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
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
Predicate finalQuery = cb.or(predicates.toArray());
But this code gave compilation error as the parameters needed is varargs i.e.
or method accepts (Predicate... predicates) as parameter list
Can you please provide some simple solution to this issue?
Try this:
Predicate finalQuery = cb.or(predicates.toArray(new Predicate[0]));