Hibernate Predicate SelectAll

1.1k views Asked by At

I'm using Spring Data JPA with a specification to select rows out of table if the user has selected certain filters:

public interface Specification { Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb); }

How do I create a predicate which will return everything?

1

There are 1 answers

0
Mikko Maunu On BEST ANSWER

As documented, conjunction with zero conjuncts is true. Such a predicate can be created as follows:

CriteriaBuilder cb ...
Predicate conjunction = cb.conjunction();

When opposite is needed, that is task for disjunction (with zero disjuncts it is false):

CriteriaBuilder cb ...
Predicate disjunction = cb.disjunction();

Disjunction without disjunct typically generates something like 1=0 to SQL query.