简体   繁体   English

标准api其中1 <> 1条款

[英]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'. 我不能不查询数据库,所以我想为谓词添加一些无法访问的条件,比如'where 1 <> 1'。 But the CriteriaBuilder.equal() doesn't allow to do that. 但CriteriaBuilder.equal()不允许这样做。 Is there any way to reach the goal? 有没有办法达成目标?

Thanks. 谢谢。

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. 虽然,如果您知道这不应该执行,那么如果数据库无法查看绑定值,那么在某些RDBMS上使用表达式可能不是最佳的。 I don't know how to do create a JPA Predicate with an inlined 1 <> 1 SQL expression, though... 我不知道如何使用内联的1 <> 1 SQL表达式创建JPA谓词,但是......

Javadoc from CriteriaBuilder: "Create a disjunction (with zero disjuncts). A disjunction with zero disjuncts is false." 来自CriteriaBuilder的Javadoc:“创建一个析取(零分离)。零分离的断开是错误的。”

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

And then generated SQL depends about implementation, Hibernate produces 0=1. 然后生成的SQL依赖于实现,Hibernate产生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. 它很短。

这个怎么样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM