from 子句中的 JPA/hibernate 子查询 [英] JPA/hibernate subquery in from clause

查看:36
本文介绍了from 子句中的 JPA/hibernate 子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 JPA 和 hibernate 作为提供者,我们有一个查询,它在 FROM 子句中包含一个带有子查询的连接,但我们收到以下错误:

We're using JPA with hibernate as the provider, we have a query that contains a join with a subquery in the FROM clause, but we get the following error:

org.hibernate.hql.ast.QuerySyntaxException:意外令牌:(附近第 1 行,第 75 列 [SELECT sd FROMcom.hp.amber.datamodel.entities.analysis.SnapshotDates sd,(选择max(x.changeDate) maxChangeDate, x.viewId, x.state FROMcom.hp.amber.datamodel.entities.analysis.SnapshotDates x WHEREx.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state GROUP通过 x.viewId, x.state) sd2 哪里 sd.viewId = sd2.viewId AND sd.state =:state AND sd.changeDate = sd2.maxChangeDate]

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 75 [SELECT sd FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates sd, (SELECT max(x.changeDate) maxChangeDate, x.viewId, x.state FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates x WHERE x.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state GROUP BY x.viewId, x.state) sd2 WHERE sd.viewId = sd2.viewId AND sd.state = :state AND sd.changeDate = sd2.maxChangeDate]

这是查询:

SELECT sd 
FROM SnapshotDates sd, 
     (SELECT max(x.changeDate) maxChangeDate, x.viewId, x.state 
      FROM SnapshotDates x
     WHERE x.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state
GROUP BY x.viewId, x.state) sd2
WHERE sd.viewId = sd2.viewId 
      AND sd.state = :state 
      AND sd.changeDate = sd2.maxChangeDate

感谢您的帮助

推荐答案

没想到HQL可以在from子句中做子查询

I did not think HQL could do subqueries in the from clause

https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html#queryhql-subqueries

注意句子:

请注意,HQL 子查询只能出现在 select 或 where 子句中.

Note that HQL subqueries can occur only in the select or where clauses.

我想您可以将其更改为本机查询并以这种方式执行.

I imagine you could change it to a native query and execute it that way.

这篇关于from 子句中的 JPA/hibernate 子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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