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

标签 java jpa spring-data-jpa spring-data jpql

假设我们有一个类 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'
)

关于java - JPA 查询 "Set does NOT contain a certain item",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54480168/

相关文章:

java - 我正在学习Head First Java,但我看不懂第544页

java - 确定 HTTP 响应的大小?

java - 检测到错误时重新启动程序

java - 当我在 JPA 代码中设置锁时,它们是在代码中还是在 DBMS 中强制执行?

java - 使用 JPA 从外国实体获取一定的值(value)

java - JPA @manyToMany 有条件

java - 查询在本地后端工作但不在部署的后端上工作

jpa - 在使用 InheritanceType.JOINED 的 JPA 实体层次结构中,与子类的所有关系都会导致父类(super class)表上的外键约束

java - Spring jpa 异常 - 发现对集合的共享引用

spring-boot - spring boot REST Api 中的一对多关系