Hibernate HQL 查询:如何在 where 子句中正确使用 ANY 函数?

发布于 2024-09-10 12:59:13 字数 804 浏览 0 评论 0原文

我正在努力理解 HQL 查询中的错误:

public List<Pats> getIds(List<String> patIds) {
    Session session = getSession();
    String hql = "from OurPats where patId = any (:patIds)";
    // String hql = "from OurPats where patId in (:patIds)";
    return session.createQuery(hql).setParameterList("patIds", patIds).list();
}

...注释掉的行工作正常,但我希望非工作 ANY 比较的功能,因为 patIds.size() 可以大于 2^15 (导致postgresql 中断)。

http://docs.jboss.org/ 来看hibernate/core/3.3/reference/en/html/queryhql.html ANY 函数应该可以工作。在其他论坛问题中,人们说不要使用上面链接中规定的 elements 函数(我尝试过 elements 并收到 IDENT 错误)。上面的代码产生一个 org.hibernate.hql.ast.QuerySyntaxException: 意外的 token: : 错误。

有什么想法吗?感谢您的帮助。

I'm struggling to understand my error in an HQL query:

public List<Pats> getIds(List<String> patIds) {
    Session session = getSession();
    String hql = "from OurPats where patId = any (:patIds)";
    // String hql = "from OurPats where patId in (:patIds)";
    return session.createQuery(hql).setParameterList("patIds", patIds).list();
}

...the commented out line works properly, but I want the functionality of the non-working ANY comparison as patIds.size() can be greater than 2^15 (causing postgresql to break).

Judging from http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html the ANY function should work. In other forum questions people say not to use the elements function as is stipulated in the above link (I've tried with elements and I get an IDENT error). The above code produces an org.hibernate.hql.ast.QuerySyntaxException: unexpected token: : error.

Any ideas? Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2024-09-17 12:59:13

据我所知, = ANY 相当于 IN (我认为它们会被优化器转换为相同的查询)。来自 PostgreSQL 文档:

9.16.4。任何/一些

表达式运算符 ANY(子查询)
表达式运算符 SOME(子查询)

右边是带括号的
子查询,必须准确返回
一栏。左边的表达式
评估并与每一行进行比较
使用给定的子查询结果
运算符,必须产生布尔值
结果。 ANY 的结果是“true”
如果获得任何真实结果。这
如果结果不为真,则结果为“假”
发现(包括特殊情况
其中子查询不返回任何行)。

SOMEANY 的同义词。 IN
相当于 = ANY

所以我不认为使用 = ANY 无论如何都能解决你的问题。

当然,我没有上下文,但您确定执行超过 2^15 OR 比较真的有意义吗?

As far as I know, = ANY is equivalent to IN (and I think they will be transformed into the same queries by the optimizer). From the PostgreSQL documentation:

9.16.4. ANY/SOME

expression operator ANY (subquery)
expression operator SOME (subquery)

The right-hand side is a parenthesized
subquery, which must return exactly
one column. The left-hand expression
is evaluated and compared to each row
of the subquery result using the given
operator, which must yield a Boolean
result. The result of ANY is "true"
if any true result is obtained. The
result is "false" if no true result is
found (including the special case
where the subquery returns no rows).

SOME is a synonym for ANY. IN is
equivalent to = ANY
.

So I don't think using = ANY will solve your problem anyway.

Of course, I don't have the context but are you sure that performing more than 2^15 OR comparisons really makes sense?

赏烟花じ飞满天 2024-09-17 12:59:13

不确定您想要“任何”而不是“在”的功能是什么意思。我所知道的每个 DBMS 都将它们视为同义词。

= ANY 不允许用于 MySQL 中的列表,仅适用于子查询。表面上你已经将方言设置为 MySQL 并且 Hibernate 知道这一点?

http://dev.mysql.com/ doc/refman/5.5/en/any-in-some-subqueries.html

当以下情况时,IN 和 = ANY 不是同义词
与表达式列表一起使用。在可以
获取表达式列表,but = ANY
不能。参见第 11.3.2 节,
“比较函数和运算符”。

Not sure what you mean by wanting the functionality of 'any' rather than 'in.' Every DBMS I know treats them as synonymous.

= ANY is not allowed for lists in MySQL, only for subqueries. Ostensibly you have your dialect set to MySQL and Hibernate knows this?

http://dev.mysql.com/doc/refman/5.5/en/any-in-some-subqueries.html

IN and = ANY are not synonyms when
used with an expression list. IN can
take an expression list, but = ANY
cannot. See Section 11.3.2,
“Comparison Functions and Operators”.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文