12

let me get straight to the point. I am using Spring Data JPA with QueryDSL in a project and I cannot figure out this myself.

I have the QueryDSL predicates in static methods that can take arguments and if the argument is not correct it should return "empty predicate" :

public static BooleanExpression byWhateverId(Long whateverId) {
  if(whateverId == null) return [insert magic here];
  // if parameter is OK return usual predicate
  return QClass.property.whateverId.eq(whateverId);
}

Now I want to be able to chain these predicates using AND/OR oprators :

someRepository.findAll(byWhateverId(someParam).and(bySomethingElseId(1));

The problem here is that at this point I don't know whether 'someParam' is null or not (of course I can check but that's a lot of IFs). I also know I can use BooleanBuilder class but that seems also like a lot of code that should not be needed.

Does anybody knows what could be inserted instead of "[insert magic here]" ??? Or maybe I am missing something somewhere...

Thanks!

user1622058
  • 453
  • 3
  • 7
  • 16

3 Answers3

18

You can return null for non matching predicates in byWhateverId and bySomethingElseId and combine the predicate via ExpressionUtils.allOf()

In your case

Predicate where = ExpressionUtils.allOf(byWhateverId(someParam), bySomethingElseId(1));
someRepository.findAll(where);
Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
  • Thanks for your reply. This looks quite nice but still I would like to chain then without any additional costs (like using ExpressionUtils).This solution is basically the same as BooleanBuilder.I really like the way of byWhateverId(1).and(bySomethingElseId(1)). But maybe I am just too strict in this matter and I should accept this :) – user1622058 Nov 24 '14 at 10:00
13

4 years old question, but anyway...

You can return sql predicate which is always true, like true=true:

public static BooleanExpression alwaysTrue() {
    return Expressions.TRUE.isTrue;
}

If you have a bunch of these the generated sql won't be super nice, so you might want to limit such usages to a minimum.

Michal Hosala
  • 5,570
  • 1
  • 22
  • 49
Madis
  • 411
  • 5
  • 7
7

Sorry, I completely forgot about this. The right solution (from my point of view) is to use BooleanBuilder.

user1622058
  • 453
  • 3
  • 7
  • 16