使用 JPQL 计算关联对象的正确方法

发布于 2024-09-15 00:02:39 字数 462 浏览 2 评论 0原文

编写此 JPA 查询的正确方法是什么?我只是猜测,因为我无法解决它或在我的 JPA 书中找到它。

Query query=em.createQuery("select m from Meeting m where count(m.attendees) = 0");
return query.getResultList();

我目前正在尝试使用 Hibernate,但出现 mysql 错误!

ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near ')=0' at line 1

What is the correct way to write this JPA query? I am just guessing as I cant work it out or find it in my JPA book.

Query query=em.createQuery("select m from Meeting m where count(m.attendees) = 0");
return query.getResultList();

I am currently trying this with Hibernate and I get a mysql error!

ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near ')=0' at line 1

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

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

发布评论

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

评论(1

泛泛之交 2024-09-22 00:02:46

要严格回答问题的标题,请使用 SIZE

Query query=em.createQuery("select m from Meeting m where size(m.attendees) = 0");
return query.getResultList();

来自 JPA 规范:

4.6.16.2 算术函数

functions_returning_numerics::=
ABS(简单算术表达式) |
SQRT(简单算术表达式)|
MOD(简单算术表达式, 简单算术表达式) |
SIZE(集合值路径表达式)

ABS 函数采用数字
参数并返回一个数字
(整数、浮点或双精度)
与参数的类型相同
功能。

SQRT 函数采用数字
参数并返回一个双精度值。

MOD函数需要两个整数
参数并返回一个整数。

SIZE 函数返回一个整数
值,元素的数量
收藏。如果集合是
空,SIZE 函数计算结果为
零。

这些函数的数字参数
可能对应于数字Java
对象类型以及原始类型
数字类型。

0 的特殊情况下,您还可以使用 IS EMPTY

4.6.11 空集合比较表达式

使用的语法
比较运算符 IS EMPTY
empty_collection_comparison_expression
如下:

collection_valued_pa​​th_expression 是 [NOT] 空

这个表达式测试是否
指定的集合
集合值路径表达式是
空(即没有元素)。

示例:

<前><代码>选择 o
FROM 订单 o
其中 o.lineItems 为空

如果有收藏价值
空集合中的路径表达式
比较表达式未知,
空比较的值
表达未知。

我会测试两者,看看哪一个最有效(检查查询计划)。

To strictly answer the title of the question, use SIZE:

Query query=em.createQuery("select m from Meeting m where size(m.attendees) = 0");
return query.getResultList();

From the JPA specification:

4.6.16.2 Arithmetic Functions

functions_returning_numerics::=
ABS(simple_arithmetic_expression) |
SQRT(simple_arithmetic_expression) |
MOD(simple_arithmetic_expression, simple_arithmetic_expression) |
SIZE(collection_valued_path_expression)

The ABS function takes a numeric
argument and returns a number
(integer, float, or double) of the
same type as the argument to the
function.

The SQRT function takes a numeric
argument and returns a double.

The MOD function takes two integer
arguments and returns an integer.

The SIZE function returns an integer
value, the number of elements of the
collection. If the collection is
empty, the SIZE function evaluates to
zero.

Numeric arguments to these functions
may correspond to the numeric Java
object types as well as the primitive
numeric types.

In the particular case of 0, you could also use IS EMPTY

4.6.11 Empty Collection Comparison Expressions

The syntax for the use of the
comparison operator IS EMPTY in an
empty_collection_comparison_expression
is as follows:

collection_valued_path_expression IS [NOT] EMPTY

This expression tests whether or not
the collection designated by the
collection-valued path expression is
empty (i.e, has no elements).

Example:

SELECT o
FROM Order o
WHERE o.lineItems IS EMPTY

If the value of the collection-valued
path expression in an empty collection
comparison expression is unknown, the
value of the empty comparison
expression is unknown.

I would test both to see which one is the most efficient (check the query plan).

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