简体   繁体   中英

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:

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> .

How can I create a Predicate (probably a BooleanExpression ) from such a case expression?

You could use a CaseBuilder as follows:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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