Can HQL Select on the result set of another query?(HQL 可以在另一个查询的结果集上选择吗?)
问题描述
HQL 可以在另一个查询的结果集上选择吗?例如:
Can HQL Select on the result set of another query? For example:
SELECT COUNT(*) FROM (SELECT * FROM Table)
我可以在 SQL 中执行此操作,但是当我在 HQL 中进行上述尝试时,它只是显示语法错误意外的标记:(第 1 行附近,第 22 列 ..."
I can do it in SQL but when I tried like above in HQL, it just showed me syntax error "unexpected token: ( near line 1, column 22 ..."
推荐答案
HQL 确实支持 子查询,但是它们只能出现在 select 或 where 子句中.您提供的示例最好在 HQL 中编写为直接语句.例如:
HQL does support subqueries, however they can only occur in the select or the where clause. The example you provide would best be wrote as a straight statement in HQL. For example:
select count(*) from table t (where table is the entity name)
如果查询涉及比 (select * from Table)
更复杂的语句,我建议将此逻辑放入视图中,然后根据该视图创建实体.
If the query involves a more complicated statement than (select * from Table)
, I would recommend putting this logic into a view and then creating an entity based off of this view.
对于支持子查询的数据库,Hibernate 支持子查询在查询中.子查询必须用括号括起来(通常是SQL 聚合函数调用).甚至相关的子查询(在外部查询中引用别名的子查询)是允许的.
For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.
示例
from DomesticCat as cat
where cat.name not in (
select name.nickName from Name as name
)
这篇关于HQL 可以在另一个查询的结果集上选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HQL 可以在另一个查询的结果集上选择吗?


- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01