JPA 查询"Set does NOT contain a certain item"



假设我们有一个包含Set<Book> books的类Person

要找到所有"有效Java"一书的人,你可以这样写:

select p from Person p left outer join fetch p.books as b where b.name='Effective Java'

现在,我怎样才能把这个查询放在头上,找到没有这本书的所有Person

select p from Person p "where p.books does not contain book b where b.name='Effective Java'"

你可以利用NOT EXISTS关键字:

select p 
from Person p
where not exists (
   select b from p.books b where b.name = 'Effective Java'
)

或者呢

 select p from Person p join p.books b where b.name <> 'Effective Java'

相关内容

最新更新