from 子句中的 JPA/hibernate 子查询
我们使用 JPA 和 hibernate 作为提供者, 我们有一个查询,其中包含与 FROM 子句中的子查询的联接,但出现以下错误:
org.hibernate.hql.ast.QuerySyntaxException:意外的标记:(附近 第 1 行,第 75 列 [SELECT sd FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates sd,(选择 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 其中 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
谢谢您的帮助
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: 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]
This is the query:
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
Thank you for helping
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Hibernate 更新 >= 6.1
从 Hibernate 6.1(2022 年 6 月 14 日发布):
请参阅 Hibernate 存储库中的单元测试文件中的示例代码
HQLTest.java
:旧版本的 Hibernate:
我不认为 HQL 可以在 from 子句中执行子查询
https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html#queryhql-subqueries
注意这句话:
我想您可以将其更改为本机查询并以这种方式执行。
Update for Hibernate >= 6.1
This is possible as of Hibernate 6.1 (released June 14th 2022):
See example code from a unit test file in the Hibernate repository
HQLTest.java
:Older Versions of Hibernate:
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
note the sentence:
I imagine you could change it to a native query and execute it that way.
你的SQL是:
您可以重写您的sql,如
查找示例
http ://www.java2s.com/Code/Java/JPA/EJBQLWhereClauseWithSubQuery.htm
我的类似示例
我
为 PQL 重写了
SQL它将被转换为
返回与 SQL 相同的结果。
将 Hebirnate 3.3.1 与 MySQL 5.0.24 结合使用
Your SQL is:
You can rewrite your sql like
Find inspired by example
http://www.java2s.com/Code/Java/JPA/EJBQLWhereClauseWithSubQuery.htm
My similar example
I had SQL
Rewrite it for PQL
It will be translated to
return same result as SQL.
Use Hebirnate 3.3.1 with MySQL 5.0.24
Hibernate 6.1 Final 支持 HQL 和 Criteria 查询的 from 子句中的子查询(包括横向子查询)
https://in.relation.to/2022/06/14/orm-61-final/
Subqueries (including lateral subqueries) in the from-clause of HQL and Criteria queries are supported from Hibernate 6.1 Final
https://in.relation.to/2022/06/14/orm-61-final/