重写HQL,将右连接转换为左连接

发布于 2024-10-13 22:20:57 字数 1039 浏览 11 评论 0原文

我有以下对象模型:

Account

AccountTransaction
  has many-to-one to Account

PaymentSchedulePayment
  has many-to-one to AccountTransaction

PaymentSchedulePeriod
  has many-to-one to Account

我想要一些 HQL 来查找给定日期内所有活动的 PaymentSchedulePeriod ,并为每个活动找到最新的 PaymentSchedulePayment code> 链接到同一帐户。因此可能没有付款,因此外部连接。

我有一个运行良好的查询:

select p.id, a.id, max( pay.accountTransaction.dttm )
from PaymentSchedulePayment pay right join pay.accountTransaction.account a,
PaymentSchedulePeriod p
where p.account = a
and p.fromDttm <= :date and p.toDttm > :date
group by p.id, a.id

但是,从这个问题开始,我以前遇到了麻烦,因为在接受的答案中解释了连接的排序优先级。其中一条评论是,为了避免此类问题,我应该使用左外连接而不是右外连接,但我不确定如何将我的 HQL 重写为

from Account left join 的 形式... PaymentSchedulePayment

我的理解(可能不完整)是,在 HQL 中,您只能通过对象模型进行外连接。

谁能建议我如何按照建议使用左连接而不是右连接重写上面的内容?

I have the following object model:

Account

AccountTransaction
  has many-to-one to Account

PaymentSchedulePayment
  has many-to-one to AccountTransaction

PaymentSchedulePeriod
  has many-to-one to Account

And I want some HQL that will find me all PaymentSchedulePeriod active on the given date and for each one find the latest PaymentSchedulePayment linked to the same account. There may be no payments hence the outer join.

I have a query that works fine:

select p.id, a.id, max( pay.accountTransaction.dttm )
from PaymentSchedulePayment pay right join pay.accountTransaction.account a,
PaymentSchedulePeriod p
where p.account = a
and p.fromDttm <= :date and p.toDttm > :date
group by p.id, a.id

However from this question I was previously having trouble because of the ordering precedence of the joins which was explained in the accepted answer. One of the comments was that to avoid issues like this I should use left outer join instead of right but I'm not sure how to rewrite my HQL to be of the form

from Account left join ... PaymentSchedulePayment

My, possibly incomplete, understanding is that in HQL you can only do outer joins via the object model.

Can anyone suggest how I can rewrite the above as suggested using left rather than right joins?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文