开发者

criteria api where 1<>1 clause

I want the query not to return any values. I can't just not query database, so I'd like to add some unreachable condition to predicates, something like 'where 1 <> 1'. But the CriteriaBuilder.equal() doesn't allow to do that. Is there any way to reach the goal?

Thanks开发者_Go百科.


How about

CriteriaBuilder.notEqual(CriteriaBuilder.literal(1), 1)

Although, if you know that this shouldn't execute, then using expressions might not be optimal on some RDBMS, if the database can't peek at bind values. I don't know how to do create a JPA Predicate with an inlined 1 <> 1 SQL expression, though...


Javadoc from CriteriaBuilder: "Create a disjunction (with zero disjuncts). A disjunction with zero disjuncts is false."

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

And then generated SQL depends about implementation, Hibernate produces 0=1. Why I prefer this method over other alternatives, is:

  1. According documentation it fits perfectly to this use without additional arguments and you kind of guess what it does from name.
  2. It is short.


How about this:

CriteriaBuilder.isTrue(CriteriaBuilder.literal(Boolean.FALSE));


criteriaBuilder.isTrue(criteriaBuilder.literal(false));

or

Specification<TheEntity> specification = (root, query, cb) -> cb.isTrue(cb.literal(false));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜