2010-04-06 44 views

回答

1

你可以做到,但它需要兩個查詢。這是因爲要按集合的大小排序,您需要使用「分組依據」,但這需要您枚舉所有主題屬性。如果您添加或刪除一個查詢將會中斷。所以,解決的辦法就是運行一個查詢發現下令IDS,第二是獲取實例爲這些ID:

String hql = ''' 
SELECT t.id 
FROM Topic t LEFT JOIN t.posts AS post 
GROUP BY t.id 
ORDER BY COUNT(post) DESC 
''' 
def ids = Topic.executeQuery(hql) 
def orderedTopics = Topic.getAll(ids)