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

查看:68
本文介绍了我可以渴望使用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天全站免登陆