khe*_*aud 5 sql nhibernate hql jpql
我尝试将我的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;
Run Code Online (Sandbox Code Playgroud)
我尝试(不成功)在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
Run Code Online (Sandbox Code Playgroud)
但我得到了提升:
Run Code Online (Sandbox Code Playgroud)IllegalArgumentException occured :org.hibernate.hql.ast.QuerySyntaxException:意外令牌:(第1行,第16列[选择pc from(select id,max(creationDate)from models.PushCampaign group by bde)temp,models.PushCampaign pc where pc.id = temp.id]
我读的嵌套选择只能在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();
Run Code Online (Sandbox Code Playgroud)