我尝试将我的SQL查询转换为HQL或JPQL(我想从对象映射中受益)。

我的SQL请求是:

SELECT *
FROM (SELECT bde, MAX(creation_date)
      FROM push_campaign GROUP BY bde) temp,
push_campaign pc where pc.bde = temp.bde and pc.creation_date = temp.creation_date;

我尝试(未成功)用JPQL将其转换为:
select pc
from (select bde, max(creationDate)
      from PushCampaign group by bde) temp,
PushCampaign pc
where pc.bde = temp.bde and pc.creationDate = temp.creationDate

但是我长大了:



我读到嵌套的select只能在select或where子句中。

您是否有变通办法来保持请求和对象映射的好处?

最佳答案

在单个请求中无法使用JPQL或HQL。

为此,我建议:

String campaignToLaunch = "select pc.* from PushCampaign pc ..."
//SQL request which return a resultset compatible with the models.PushCampaign class
Class className = Class.forName("models.PushCampaign");
List<PushCampaign> result = JPA.em()
                           .createNativeQuery(campaignToLaunch,className)
                           .getResultList();

关于sql - HQL/JPQL-在FROM上嵌套选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6959799/

10-13 09:43