4

我正在尝试解决如何在以下 HQL 查询中急切加载客户:

select order.Customer
from Order as order
where order.Id in
(
  select itemId
  from BadItem as badItem
  where (badItemType = :itemType) and (badItem.Date >= :yesterday)
)

订单和客户之间通常存在多对一的关系。

如果可能的话,我想在查询中执行此操作,而不是在映射中 - 如在“join fetch ...”中

也许查询会被重构为一个连接,我有一个心理障碍。

有任何想法吗?

4

1 回答 1

9
select customer
from BadItem as badItem
join fetch badItem.Order as order
left join fetch order.Customer as customer 
where (badItemType = :itemType) and (badItem.Date >= :yesterday)

为此,如果 BadItem 与 Order 无关,则需要添加 BadItem 和 Order 之间的关系,或者使用inner join带有额外条件的 an(使用备选方案 2 时要注意性能)。

选择:

select customer
from Order as order
join fetch order.Customer as customer
where order.Id in
(
  select itemId
  from BadItem as badItem
  where (badItemType = :itemType) and (badItem.Date >= :yesterday)
)

编辑:

关于什么:

select customer
from Order as order
join fetch order.Customer customer
join fetch customer.orders 
where order.Id in
(
  select itemId
  from BadItem as badItem
  where (badItemType = :itemType) and (badItem.Date >= :yesterday)
)
于 2010-09-10T12:10:43.410 回答