2010-06-24 109 views
1

我有以下結構的表:休眠HQL getMaxVersion

ReportId

標題
.....

我想用HQL獲取的最新版本按ID報告。以下查詢會起作用嗎?

from Report where reportId = :reportId and version = (select max(version) from Report where reportId = :reportId) 

是否有可能檢索最大版本的行而不使用子選擇?上面的子選擇在Hibernate中是否合法?

回答

0

嘗試此查詢:

from Report where reportId = :reportId order by version desc 

與setMaxResults = 1

+0

這包括用例的99%,但如果我要檢索標題的列表中的所有的報告呢?例如,帶有可由系統生成的所有報告標題的下拉菜單。 – David 2010-06-24 16:31:18

+0

SELECT reportId,title,max(version)FROM report GROUP BY reportId。但是在這種情況下你必須使用投影。 – uthark 2010-06-24 16:49:42

0

您可以按版本進行排序並在查詢中使用setMaxResults(將其設置爲1)。至於另一個問題,像這樣的子查詢在hql中是合法的。