[英]QueryDSL: How can I create a predicate from a case expression?
Using QueryDSL, I want to create a Predicate
from a case expression that returns boolean expressions: 我想使用QueryDSL,从返回布尔表达式的case表达式中创建一个
Predicate
:
public static Predicate examplePredicate(NumberPath<Integer> typePath, DateTimePath<Date> timestampPath) {
final Expression<Boolean> expr = typePath
.when(0).then(timestampPath.lt(currentTimestamp()))
.when(1).then(timestampPath.goe(currentTimestamp()))
.otherwise(false);
// How can I create a predicate from expr?
}
All then
clauses of the case expression return BooleanExpression
s - which implements Predicate
- but the expr
itself is of type Expression<Boolean>
. 案例表达式的所有
then
子句都返回BooleanExpression
s-实现Predicate
但expr
本身的类型为Expression<Boolean>
。
How can I create a Predicate
(probably a BooleanExpression
) from such a case expression? 如何从这样的case表达式创建
Predicate
(可能是BooleanExpression
)?
You could use a CaseBuilder
as follows: 您可以按以下方式使用
CaseBuilder
:
new CaseBuilder()
.when(typePath.intValue().eq(0))
.then(timestampPath.lt(currentTimestamp()))
...
As noted in the comments to the original question, this generates different SQL to that generated by the original code extract but performs the desired function. 如对原始问题的注释中所述,这会生成与原始代码提取所生成的SQL不同的SQL,但会执行所需的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.