0

I have this examples ,but I can't solved difference between them. Both of them are creating same query.

public static Specification<TransactionProjection> hasDestinationPartyNumber(List<PartyNumber> values) {
    if (CollectionUtils.isEmpty(values)) return not();
    return (root, query, cb) -> cb.isTrue(root.get(TransactionProjection_.column1).in(values));
}

public static Specification<TransactionProjection> hasNotDestinationPartyNumber(List<PartyNumber> values) {
    if (CollectionUtils.isEmpty(values)) return not();
    return (root, query, cb) -> cb.isFalse(root.get(TransactionProjection_.column1).in(values));
}
Samet Baskıcı
  • 1,090
  • 3
  • 13
  • 26
Rhmn61
  • 200
  • 2
  • 12
  • I think the reason might be because logical tests in SQL do not have an intrinsic boolean value. Try using `root.get(TransactionProjection_.column1).in(values)` directly for the first `Predicate` and `cb.not(...)` instead of `cb.isFalse(...)` for the second one – crizzis Aug 01 '18 at 17:32
  • @crizzis I didn't understand what do you mean logical tests in SQL do not have intrinsic boolean value ? ı try cb.not () and thats work.but ı am wondering what is the difference between isFalse and isTrue ? – Rhmn61 Aug 01 '18 at 17:58
  • See my answer for an explanation – crizzis Aug 02 '18 at 07:58

1 Answers1

1

Note that I haven't tested my answer, and isTrue(...)/isFalse(...) is poorly documented.

I think the reason why isTrue(...)/isFalse(...) has no effect in your example is that logical tests in SQL (and, by extension, in JPQL/Criteria API, in order for it to be easily translatable to SQL) do not have a boolean value (in fact, they are not r-values at all). For instance, the expression WHERE (a IN (1, 2, 3)) = TRUE is invalid, since the a IN (1, 2, 3) part does not evaluate to anything.

isTrue(...)/isFalse(...) is meant to be applied to expressions that have an actual value. For example, if the TransactionProjection entity had a boolean active property, then it would make sense to use cb.isTrue(root.get(TransactionProjection_.active)) or cb.isFalse(root.get(TransactionProjection_.active)).

I'm surprised that a query syntax exception wasn't thrown in your example, though, it might be a bug of the JPA implementation.

crizzis
  • 9,978
  • 2
  • 28
  • 47