FROM子句中的JPA子查询可能吗? [英] Is JPA subquery in FROM clause possible?

查看:117
本文介绍了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添加了另一列称为数量",并且每次我从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子查询可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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