JPA - MAX of COUNT 或 SELECT FROM SELECT [英] JPA - MAX of COUNT or SELECT FROM SELECT

查看:31
本文介绍了JPA - MAX of COUNT 或 SELECT FROM SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I wrote the following query for MySQL:

SELECT subquery.t1_column1, 
    subquery.t2_id, 
    MAX(subquery.val)
FROM (
    SELECT t1.column1 as t1_column1, 
        t1.id_t2 AS t2_id,
        count(1) AS val
    FROM table1 t1
    INNER JOIN table2 t2
    ON t2.id = t1.id_t2
    GROUP BY t1.id_t2
) subquery
GROUP BY t1_column1

And I'd like to translate it into JPA (JPQL or criteria query).

I don't know how to make this max(count) thing, and JPA doesn't seem to like the SELECT FROM SELECT...

If anyone has an idea other than native queries (I'll do it for now), it would be great.

解决方案

I haven't checked tha JPA specification, but given that the Hibernate documentation says

Note that HQL subqueries can occur only in the select or where clauses.

I very much doubt that your query can be transformed in a valid JPQL query.

You'll have to keep using this native SQL query.

这篇关于JPA - MAX of COUNT 或 SELECT FROM SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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