且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从sql查询中获取jpql查询?

更新时间:2023-02-26 10:02:25

这是一个较旧的文章,但是如果它对其他人有帮助,我只想补充一点,当前版本的JPQL不支持子查询.在FROM子句中.在JPQL参考中:

This is an older post, but in case it helps anyone else out, I'll just add that the current version of JPQL doesn't support sub-queries in the FROM clause. From the JPQL Reference:

在此版本中,子查询仅限于WHERE和HAVING子句.在规范的更高版本中,将考虑对FROM子句中子查询的支持.

Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.

当我必须在项目上执行类似操作时,必须将聚合子查询下移到where子句中,将Comparator添加到实体,然后在JPQL调用之后执行Collections.sort.因此查询将类似于以下内容:

When I had to do something similar on a project, I had to move the aggregate sub-query down into the where clause, add a Comparator to the entity, and then do a Collections.sort after the JPQL call. So the query would become something along the lines of:

...
WHERE p2.typeCuisson IN (SELECT G.TYPEC 
FROM PlanningCuisson G 
GROUP BY G.TYPEC 
HAVING COUNT(G.TYPEC) > 0)...

query.getResultList()