How can I build a predicate filtering to where ALL tags in an array exist, joined to a record, using criteriaBuilder?

Issue

Here’s an example of the syntax I’m using for another condition (where ANY of the tags appear on the document via a FK).

predicates.add(root.join(Document_.tags).in({ pseudocode array of tags }));

I’m trying to come up with a similar predicate, but where the Document entity has ALL of the tags listed in the filter.

Solution

if all the tags listed in the filters are all the tags that exist you could try this

predicates.add(criteriaBuilder.equal(criteriaBuilder.size(root.get(Document_.tags)),
    countOfAllTheTags));

or, if the tags in the filter are a subset of all of the tags, try this one

predicates.addAll(Stream.of(tags)
    .map(tag -> criteriaBuilder.isMember(tag, root.get(Document_.tags)))
    .collect(Collectors.toList());

Answered By – tremendous7

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published