0

我在休眠中有以下 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) 也能正常工作?

4

1 回答 1

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)
于 2013-07-09T20:06:57.030 回答