HQL 可以在另一个查询的结果集上选择吗? [英] Can HQL Select on the result set of another query?

查看:38
本文介绍了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 可以在另一个查询的结果集上选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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