一尘不染

子句中的JPA /休眠子查询

hibernate

我们正在将JPA与hibernate用作提供程序,我们有一个查询,该查询包含一个与FROM子句中的子查询的联接,但是会出现以下错误:

org.hibernate.hql.ast.QuerySyntaxException:意外令牌:(在第1行第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(.viewIds)中的x.viewId和x.state =:state由x.viewId组成的GROUP
x.state)sd2,其中sd.viewId = sd2.viewId和sd.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

谢谢你的帮忙


阅读 280

收藏
2020-06-20

共1个答案

一尘不染

我不认为HQL可以在from子句中执行子查询

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

注意这句话:

请注意,HQL子查询只能在select或where子句中发生。

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

2020-06-20