我可以使用 HQL 预先加载属性吗? [英] Can I eager load a property using HQL?

查看:24
本文介绍了我可以使用 HQL 预先加载属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究如何在以下 HQL 查询中预先加载客户:

I'm trying to work out how to eager load the customers in the following HQL query:

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

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

There's the usual many-to-one relationship between orders and customers.

如果可能的话,我想在查询中执行此操作,而不是在映射中 - 就像在join fetch..."中一样

I'd like to do this is in the query, as opposed to the mapping, if possible - as in a "join fetch..."

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

Maybe the query would be refactored as a join and I'm having a mental block.

有什么想法吗?

推荐答案

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(使用替代方案 2 时注意性能).

For this to work you will need to add the relationship between BadItem and Order if BadItem is unrelated to Order, or use an inner join with extra conditions (watch out for performance when using alternative 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)
)

这篇关于我可以使用 HQL 预先加载属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆