使用 QueryDSL,我想创建一个 Predicate
来自返回 boolean 表达式的 case 表达式:
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?
}
全部then
case 表达式的子句返回 BooleanExpression
s - 实现 Predicate
- 但是expr
其本身类型为 Expression<Boolean>
.
如何创建 Predicate
(可能是 BooleanExpression
)来自这样的 case 表达式?
最佳答案
您可以使用 CaseBuilder
如下:
new CaseBuilder()
.when(typePath.intValue().eq(0))
.then(timestampPath.lt(currentTimestamp()))
...
正如原始问题的评论中所述,这会生成与原始代码摘录生成的 SQL 不同的 SQL,但会执行所需的功能。
关于java - QueryDSL:如何从 case 表达式创建谓词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253002/