【问题标题】:How to query for a Customer that has orders of a certain type OR no orders at all?如何查询具有某种类型订单或根本没有订单的客户?
【发布时间】:2016-06-25 14:12:00
【问题描述】:

我知道如何查询具有特定类型订单的客户(在使用 leftjoinfetch 加入订单后):

select c from Customer c where c.orders.type = 'CANCELLED' 

我也知道如何查询没有订单的客户:

select c from Customer c where c.orders is empty 

如何在一个查询中选择所有具有某种订单类型或根本没有订单的客户?

谢谢。

【问题讨论】:

  • 我看不到您在所示语句的任何地方使用的任何左连接提取。此外,c.orders.type 将在某些 JPA 提供程序(如 Hibernate)上生成笛卡尔积(交叉连接)。

标签: java jpa select left-join jpql


【解决方案1】:

类似的东西(在没有实际实体的情况下)。

SELECT c FROM Customer c LEFT JOIN c.orders o WHERE c.orders IS EMPTY OR o.type = 'CANCELLED'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多