本文介绍了FROM 子句中的 JPA 子查询是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 JPA 时遇到了一点问题.考虑这种情况:

I'm having a little problem with JPA. Consider this scenario:

表 A (id_a) |表 B (id_b, id_a)

我需要的是这样的查询:

What I need is a query like this:

Select a.*, c.quantity from A as a, (Select Count(*) as quantity
from B as b where b.id_a = a.id_a) as c;

问题是我想使用 jpa 查询而不是本机查询,如下所示:

The thing is that I want to use a jpa query and not a native query, something like this:

Select a, c FROM A a, (Select Count(b) FROM B b where a.idA = b.a.idA) c;

然后我可以从结果(每个节点中带有 a 和 c 的 Object[] 列表)进行迭代,然后分配 a.quantity = c;

So then I could iterate from the result (a list of Object[] with a and c in each node) and then assign a.quantity = c;

我再说一遍,我不想使用本机查询,但我发现除了使用冗余数据之外别无他法,并在 A 中添加另一个名为 Quantity 的列,每次我从 B 插入和删除时,更新 A 中的这一列.

I repeat, I don't want to use native query, but I found no other way than use redundant data, and add another column to A called Quantity and every time I insert and delete from B, update this column in A.

请帮忙,我在某处读到 JPA 不接受 Form 子句中的子查询,那么,我该怎么办?

Please help, I read somewhere that JPA doesn't accept subqueries in Form clause, so, what can I do ?

非常感谢!

推荐答案

JPA 不支持 FROM 子句中的子选择,但 EclipseLink 2.4 当前里程碑版本确实支持.

JPA does not support sub-selects in the FROM clause but EclipseLink 2.4 current milestones builds does have this support.

看,http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause

不过,您或许可以只使用普通连接来重写查询.

You can probably rewrite the query with just normal joins though.

也许,

Select a, size(a.bs) from A a

Select a, count(b) from A a join a.bs b group by a

这篇关于FROM 子句中的 JPA 子查询是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:53