假设我在产品和客户之间存在双向ManyToMany关系。现在我列出了与某个产品无关的所有客户的清单。我无法找到如何制作最聪明,性能最佳的JPA查询...结果SQL查询应该如下所示:
SELECT * FROM CUSTOMER c
WHERE c.CUSTOMER_ID NOT IN
(SELECT cp.CUSTOMER_ID FROM PRODUCT p, CUSTOMER_PRODUCT cp
WHERE p.PRODUCT_ID = cp.PRODUCT_ID
AND p.PRODUCT_ID = 1234567)
答案 0 :(得分:1)
只需使用:
select c from Customer c where c.customer_id not in (select c.customer_id from Customer c join c.products p where p.product_id = 123567