本文介绍了使用SelectQuery对象构建嵌套条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用SelectQuery对象构建嵌套条件吗?

Can I build nested conditions using the SelectQuery object?

我想得到:

select *
from car
where ((car.color = 'blue' or car.model = 'Genesis')
or (car.manufactor = 'ford' and car.color = 'blue'))

推荐答案

如果内联所有条件/谓词...

...然后您可以像这样将它们提供给SelectQuery对象:

SelectQuery query = ...
query.addConditions( ( CAR.COLOR.eq("blue") .or ( CAR.MODEL.eq("Genesis") ) )
                 .or ( CAR.MANUFACTOR.eq("ford") .and ( CAR.COLOR.eq("blue") ) ) )

我添加了一些空格以提高可读性

I've added some whitespace for improved readability

...然后,您可以按照以下步骤创建谓词:

... Then you can create your predicate in various steps:

Condition condition;

condition = CAR.COLOR.eq("blue");
condition = condition.or(CAR.MODEL.eq("Genesis"));
condition = condition.or(CAR.MANUFACTOR.eq("ford").and(CAR.COLOR.eq("blue")));

这两种方法是完全等效的.

Both approaches are completely equivalent.

这篇关于使用SelectQuery对象构建嵌套条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:50