如何在 JPQL 中执行复杂的 LEFT JOIN 条件? [英] How to do a complex LEFT JOIN condition in JPQL?

查看:48
本文介绍了如何在 JPQL 中执行复杂的 LEFT JOIN 条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JPQL 进行来自 Play Framework 的模型查询.

I am using JPQL for the model queries from Play Framework.

我想知道 JPQL 是否支持 LEFT JOIN 的复杂"ON 条件.

I wonder if JPQL supports "complex" ON condition for LEFT JOIN.

在我的一个例子中,有 2 个表:

In an example I have, there are 2 tables:

  • '应用' - 应用列表
  • 'AggregationHistory' - 每个应用程序、每个日期的聚合记录列表.在模型中,它有app"字段代表多对一与'App'(物理表中的列名'app_id')

假设我想计算没有特定日期记录的所有应用的列表.

Suppose I want to calculate the list of all the apps that do not have a record for a specific date.

在普通 SQL 中,我使用以下查询获得结果:

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

ON"条件下的AND"当然是必不可少的.

'AND' in 'ON' condition is essential, of course.

现在,我看到的所有 JPQL 示例都是这样的:

Now, all the examples of JPQL I see are like:

SELECT a.* FROM AggregationHistory ah  LEFT JOIN ah.app a WHERE ...

那么,唯一支持的ON"条件是 ID 的匹配"吗?(WHERE 似乎对我帮助不大)

So, the only 'ON' condition supported - is the "match" of the IDs? (WHERE seems not to help me much)

我可以想到解决方法(例如,使用本机查询",或使用 JOIN 获取确实具有记录的应用程序列表,然后进行比较).但是我想知道我在SQL中所做的查询是否可以转换为JPQL.

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.

谢谢

推荐答案

JPQL,AFAIK,不支持 ON 子句,但 HQL 支持它们.选择使用 with 关键字:

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

这篇关于如何在 JPQL 中执行复杂的 LEFT JOIN 条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆