简体   繁体   English

JPA-条件构建器-和(x或y)

[英]JPA - Criteria Builder - and( x or y)

I'd like to add a filtercondition to my criteria query with: cb.and(filtercondition or filterCondition) 我想使用以下方法向我的条件查询中添加一个filtercondition:cb.and(filtercondition或filterCondition)

so that it's something like: 就像这样:

select * from table t where t.name = 'test' AND (t.place = 'test' OR 'test2');

I've tried the following: 我尝试了以下方法:

public static Predicate getPredicateForTwoExpressionsAsOrCondition(String filterValue, CriteriaBuilder cb, Expression<String> firstExpression, Expression<String> secondExpression, Predicate filterCondition) {
        Predicate or = cb.or(filterCondition, cb.like(cb.lower(secondExpression), "%" + filterValue.toLowerCase() + "%"));
        return cb.and(or, cb.like(cb.lower(firstExpression), "%" + filterValue.toLowerCase() + "%"));
}

but now it seems as if he's only taking the firstExpression into account. 但现在看来,他似乎只考虑了firstExpression。

note that the above method builds on previous filterConditions.. That's why I'm passing it as a parameter. 请注意,上述方法建立在以前的filterConditions之上。这就是为什么我将其作为参数传递的原因。

Using JPA 2.0 and kind of new with the criteria builder. 使用JPA 2.0和带有条件构建器的新功能。

Found it by looking at CriteriaBuilder.and & CriteriaBuilder.or, how-to? 通过查看CriteriaBuilder.and&CriteriaBuilder。找到它,或者如何做?

fix: 固定:

Predicate or = cb.or(cb.like(cb.lower(secondExpression), "%" + filterValue.toLowerCase() + "%"), cb.like(cb.lower(firstExpression), "%" + filterValue.toLowerCase() + "%"));
return cb.and(filterCondition, or);

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

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