users@jpa-spec.java.net

[jpa-spec users] Re: criteria api missing where(List<Predicate>)

From: Linda DeMichiel <linda.demichiel_at_oracle.com>
Date: Wed, 23 Jan 2013 08:52:17 -0800

On 1/22/2013 10:47 PM, Yannick Majoros wrote:
> Hello,
>
> I opened JPA_SPEC-44, which was closed (http://java.net/jira/browse/JPA_SPEC-44).
>
> It was about the criteria query API missing a where() method with a List parameter:
>
> CriteriaQuery<T> where(List<Predicate> restrictions);
>
> Although groupBy, having, orderBy etc. all have both array (varargs) and List parameter versions.
>
> The issue was closed with this comment:
> "It was felt that CriteriaQuery<T> where(List<Predicate> restrictions) was not needed, as unlike in the other cases,
> there are operations to combine expressions using the logical operators."
>
> However, I think it would be a good idea to include it. I'm using the criteria API like this:
>
> List<Predicate> predicates = new ArrayList<>();
> ClientStatus searchedClientStatus = clientSearch.getClientStatus();
> if (searchedClientStatus != null) {
> Predicate clientStatusPredicate = ....;
> predicates.add(clientStatusPredicate);
> }
> String searchedCity = clientSearch.getCity();
> if (searchedCity != null) {
> Predicate cityPredicate = ....;
> predicates.add(cityPredicate);
> }
>
> I then have to do this:
> Predicate[] predicateArray = predicates.toArray(new Predicate[0]);
> clientQuery.where(predicateArray);
>
> I think it would be easier, and the API would be more consistent, if we could just do this:
> clientQuery.where(predicates);
>
> What do you think?
>

Have you considered using the CriteriaBuilder and method to additively build up your predicates?

e.g.
predicates = cb.and(predicates, cityPredicate);
...



> Best regards,
>
> Yannick Majoros