在 JPQL 中按 COUNT 排序
Order by COUNT in JPQL
我有 News
实体与 Comments
存在一对多关系。
我的目标是根据评论数量对 News
进行排序。
以下查询有效,但显然 returns 只有 ID,而不是所有 News
' 字段:
SELECT news.id
FROM News news JOIN news.comments comments
GROUP BY news
ORDER BY COUNT(comments) DESC
如果我将 news.id
替换为 news
,则会出现错误:
SqlExceptionHelper:146 - ORA-01034: ORACLE not available
如何在单个查询中获取整个排序的 News
实体,而不仅仅是 ID?
(数据库:Oracle XE,持久化提供者:Hibernate)
作为@thanhnguyen said:
If you pass an entity inside the GROUP BY, Hibernate automatically adds its id to the transformed SQL of the underlying DB. In addition, the values in the GROUP BY must exist in the SELECT clause. Thus, instead of select the whole object, you can select its id, then from those ids, you can retrieve the object again.
这可能表明你想做的事情是不可能的。
我有 News
实体与 Comments
存在一对多关系。
我的目标是根据评论数量对 News
进行排序。
以下查询有效,但显然 returns 只有 ID,而不是所有 News
' 字段:
SELECT news.id
FROM News news JOIN news.comments comments
GROUP BY news
ORDER BY COUNT(comments) DESC
如果我将 news.id
替换为 news
,则会出现错误:
SqlExceptionHelper:146 - ORA-01034: ORACLE not available
如何在单个查询中获取整个排序的 News
实体,而不仅仅是 ID?
(数据库:Oracle XE,持久化提供者:Hibernate)
作为@thanhnguyen said:
If you pass an entity inside the GROUP BY, Hibernate automatically adds its id to the transformed SQL of the underlying DB. In addition, the values in the GROUP BY must exist in the SELECT clause. Thus, instead of select the whole object, you can select its id, then from those ids, you can retrieve the object again.
这可能表明你想做的事情是不可能的。