Dancyg
Dancyg

Reputation: 159

Hibernate error: unexpected AST node

I need to sort topics by latest post. Could someone please help me with this hibernate query:

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]

What is the issue here?

Upvotes: 0

Views: 2139

Answers (2)

RubioRic
RubioRic

Reputation: 2468

I think that this JPQL query should work

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

If you want to include topics that have not related posts.

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

You have to specify the selected fields from Topic in order to make the aggregation function max work. Hope this helps.

Upvotes: 0

Khoda
Khoda

Reputation: 953

SELECT topic FROM Post ORDER BY createdOn

Upvotes: 1

Related Questions