重写HQL,将右连接转换为左连接
我有以下对象模型:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论