如何防止Hibernate中出现空列表错误?

时间:2013-07-09 19:49:49

标签: spring hibernate grails

我在hibernate中有以下HQL查询:

select x 
from Item as x
where x not in (select o.item from Opinion as o where o.user =:user)

除非子查询(select o.item from Opinion as o where o.user =:user)返回空列表,否则此查询正常工作。在这种情况下,我收到了一个错误。

当子查询(select o.item from Opinion as o where o.user =:user)为空时,有没有办法阻止Hibernate引发错误?

如何重写查询以使其在空(select o.item from Opinion as o where o.user =:user)下工作?

1 个答案:

答案 0 :(得分:0)

我看到的一个微不足道的方式是

select x 
from Item as x
where (select count(distinct o.item) from Opinion as o where o.user =:user) = 0

或(*未经测试)

select x 
from Item as x
where not exists (from Opinion as o where o.item = x and o.user =:user)