ManyAoMany的JPA查询,其中A hat与B无关

时间:2014-08-14 13:01:08

标签: jpa

假设我在产品和客户之间存在双向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)

1 个答案:

答案 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