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 12:00:07 -0500

Linda,

Atleast in my case, that assumes that you have one predicate already
defined. In my setup I have no predicates guaranteed defined. This would
end up happening:

Predicate p = null;

p = cb.and(p,cityPredicate)

Should that work?


On Wed, Jan 23, 2013 at 11:52 AM, Linda DeMichiel <
linda.demichiel_at_oracle.com> wrote:

>
>
> 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 <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
>>
>