本文介绍了在 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

这是查询:

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 子句中.

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

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

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

09-03 08:32