users@jpa-spec.java.net

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

From: John D. Ament <john.d.ament_at_gmail.com>
Date: Wed, 23 Jan 2013 07:23:34 -0500

+1 from me as well. This is often how I build my where clauses in criteria
api.


On Wed, Jan 23, 2013 at 1:47 AM, Yannick Majoros
<yannick.majoros_at_gmail.com>wrote:

> Hello,
>
> I opened JPA_SPEC-44, which was closed (http://java.net/jira/browse/**
> JPA_SPEC-44 <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?
>
> Best regards,
>
> Yannick Majoros
>