Spring with Query Dsl custom binding and or operation doesn't work Spring with Query Dsl custom binding and or operation doesn't work spring spring

Spring with Query Dsl custom binding and or operation doesn't work


QuerydslBindings.TypeBinder#first uses a single value binding. You need to use QuerydslBindings.TypeBinder#all. This method operates on a multivalue binding.

I provided several examples of how to customize your bindings in this answer. Something like this should work for you:

@Overridedefault public void customize(QuerydslBindings bindings, QArticle article) {    // using explicit path bindings    bindings.bind(article.category).all((path, values) -> {        BooleanBuilder predicate = new BooleanBuilder();        // with a for loop        for (String value : values) {            predicate.or(path.containsIgnoreCase(value));        }    });    // using a type binding    bindings.bind(String.class).all((StringPath path, Collection<? extends String> values) -> {        BooleanBuilder predicate = new BooleanBuilder();        // oneliner with Java 8 forEach        values.forEach( value -> predicate.or(path.containsIgnoreCase(value) );    });}


The correct bindings for your article.category property should be like this:

bindings.bind(article.category).all((path, value) -> {    BooleanBuilder predicate = new BooleanBuilder();    value.forEach(o -> predicate.or(path.equalsIgnoreCase(o)));    return Optional.of(predicate);});

If you don't need any manipulations of the parameters (like ignoring case) you can simplify it to that:

bindings.bind(article.category).all((path, value) -> Optional.of(path.in(value)));


After a good search on it I found the solution.

 bindings.bind(String.class).all(new MultiValueBinding<StringPath, String> () {        @Override        public Predicate bind(StringPath path, Collection<? extends String> values) {            BooleanBuilder predicate = new BooleanBuilder();            values.forEach( value -> predicate.or(path.containsIgnoreCase(value)));            return predicate;        }    });