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?