问题
I am using JPQL for the model queries from Play Framework.
I wonder if JPQL supports "complex" ON condition for LEFT JOIN.
In an example I have, there are 2 tables:
- 'App' - list of applications
- 'AggregationHistory' - the list of the aggregation records per application, per date. In the model, it has 'app' field representing many-to-one with 'App' (column name 'app_id' in the physical table)
Suppose I want to calculate the list of all the apps that do not have a record for a specific date.
In plain SQL, I get the result using the following query:
SELECT a.* FROM app a LEFT JOIN aggregationhistory ah
ON a.id = ah.app_id AND ah.fromDate = '2012-03-14 00:00:00'
WHERE ah.fromDate is NULL
'AND' in 'ON' condition is essential, of course.
Now, all the examples of JPQL I see are like:
SELECT a.* FROM AggregationHistory ah LEFT JOIN ah.app a WHERE ...
So, the only 'ON' condition supported - is the "match" of the IDs? (WHERE seems not to help me much)
I can think of workarounds (like, using a "native query", or using JOIN to get the list of the applications that do have a record, and compare). But I wonder if the query I made in SQL - can be converted into JPQL.
Thanks
回答1:
JPQL, AFAIK, doesn't support ON
clauses, but HQL does support them. The chose to use the with
keyword though:
select a from App a
left join a.history h with h.fromDate = :fromDate
where h.fromDate is null
来源:https://stackoverflow.com/questions/9710252/how-to-do-a-complex-left-join-condition-in-jpql