0

我正在尝试在子查询中使用 case 语句编写 hql 查询。

select zr 
from ZipResource zr 
inner join zr.zipInflows zi 
inner join zi.toInstInflows tii 
inner join tii.toInstance ti 
where ti.state = 'COMPLETED' 
and 
ti.completedDate between :dateFrom and 
:dateTill 
and (
case when :units is not null then
( ti.toPrototype.unit in :units) end ) 
order by tii.zipInflow.zipResource.name

做这样的事情是真的吗?在这个查询中,我在 case 语句中得到了 QuerySyntaxException。谁能解释我做错了什么?

4

1 回答 1

0

在你的代码:units中可能是一个集合(或类似这样的东西),因为in语句需要一个集合,如果没有,它就没有意义。但是在when条件:units is not null:units必须只有一个值(或NULL),如果不是,则语法不正确。

如果你想检查是否:units为空,那么你需要第二个参数,例如:number_of_units,你检查像

and (
   case when :number_of_units != 0 then
   ( ti.toPrototype.unit in :units) end ) 

顺便说一句,你的case说法是多余的。您可以使用简单的条件来处理它and(这是值得推荐的,因为在查找搜索索引时数据库可以更好地处理和条件):

and :number_of_units != 0
and ti.toPrototype.unit in :units 

然后还有一个更简单的可能性,因为在 SQL 中<expression> in (NULL)计算结果为 false。简单地做

and ti.toPrototype.unit in :units 
于 2013-03-19T10:32:16.237 回答