QueryDSL AND/OR 运算符不适用于 BooleanExpression

sar*_*nau 5 querydsl

我们正在对实体进行搜索,我们正在使用 Query DSL。

表结构如下

TableA : shop -> has manytoOne relationship to TableB : Discount
Run Code Online (Sandbox Code Playgroud)

我们需要建立一个谓词,它返回所有没有打折
Sale 和打折Sale的商店。

我们使用 MySQL 数据库和 JPA 作为我们的持久性框架。

场景是我们正在执行搜索,搜索应该找出所有没有折扣的商店和批准折扣的商店。

下面是我们目前拥有的布尔表达式。

BooleanExpression A = shop.id.eq(SomeId);
BooleanExpression B = shop.name.eq(SomeName)
BooleanExpression C = shop.discount.isNotNull;
BooleanExpression D = shop.discount.isNull;
BooleanExpression E = shop.disccount.approved.eq(SomeValue)
Run Code Online (Sandbox Code Playgroud)

现在我们需要建立查询来获取所有没有折扣的商店,以及所有有折扣和 Approved 的商店。

我们尝试使用谓词

A
.and(B)
.and(D .or(C.and(D).and(E))
)
Run Code Online (Sandbox Code Playgroud)

我们希望查询是

where shop.id=#someid and shop.name = 'some name' and (shop.discount is Null Or (shop.discount is not null and shop.approved='#some Value'))
Run Code Online (Sandbox Code Playgroud)

但是生成的查询是什么

where  shop.id=`#someid` and shop.name = `'some name'` and (shop.discount is Null Or shop.discount is not null and shop.approved='`#some Value`')
Run Code Online (Sandbox Code Playgroud)

我们没有用这个谓词得到正确的结果集,

有什么方法可以重写谓词以使其按预期工作吗?请帮助我提出建议。

谢谢萨拉瓦纳。

Tim*_*per 2

A.and(B).and(D .or(C.and(D).and(E)))
Run Code Online (Sandbox Code Playgroud)

相当于

A and B and (D or C and D and E)
Run Code Online (Sandbox Code Playgroud)

请参阅此处了解 MySQL 运算符优先级https://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

关于你的例子,这应该有效

shop.discount.isNull()
.or(shop.discount.isNotNull()
    .and(shop.discount.approved.eq(SomeValue)))
Run Code Online (Sandbox Code Playgroud)