Hibernate错误:意外的AST节点

时间:2016-07-31 10:16:39

标签: java mysql hibernate

我需要按最新帖子排序主题。有人可以帮我解决这个hibernate查询:

unexpected AST node: query 
[SELECT t 
FROM Topic t 
ORDER BY 
(SELECT MAX(p.createdOn) FROM Post p WHERE p.topic.id = t.id) 
DESC]

这里有什么问题?

2 个答案:

答案 0 :(得分:1)

SELECT topic FROM Post ORDER BY createdOn

答案 1 :(得分:0)

我认为这个JPQL查询应该可行

select t.id, t.description, max(p.createdOn) as maxCreationTime 
from Topic t inner join t.posts p
group by t.id, t.description
order by maxCreationTime

如果您想要包含与帖子无关的主题。

select t.id, t.description, max(p.createdOn) as maxCreationTime
from Topic t left join t.posts p
group by t.id, t.description
order by maxCreationTime

您必须从主题中指定所选字段才能使聚合函数max起作用。希望这会有所帮助。