简体   繁体   English

QueryDSL:如何从案例表达式创建谓词?

[英]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-实现Predicateexpr本身的类型为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(currentTimes‌​tamp()))
...

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.

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