"Set does NOT contain a certain item" 的 JPA 查询
JPA Query for "Set does NOT contain a certain item"
假设我们有一个 class Person
其中包含 Set<Book> books
.
要找到拥有本书"Effective 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'
假设我们有一个 class Person
其中包含 Set<Book> books
.
要找到拥有本书"Effective 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'