sql-server
  • hibernate
  • hql
  • 2014-03-01 76 views 0 likes 
    0

    我需要獲取給定objectUuid的Task對象的最新「版本」。該任務由其objectUuid,taskName和createdTimestamp屬性標識。HIbernate + MSSQL查詢兼容性

    我有以下HQL查詢:

    select new list(te) from " + TaskEntity.class.getName() + " te 
    where 
    te.objectUuid = '" + domainObjectId + "' and 
    te.createdTimestamp = ( 
        select max(te.createdTimestamp) from " + TaskEntity.class.getName() + " teSub 
        where teSub.objectUuid = te.objectUuid and teSub.taskName = te.taskName 
    ) 
    

    跑和生產上的H2(嵌入式)和MySQL正確的結果。 然而安裝在生產這個代碼MS SQL Server之後我收到以下錯誤:

    An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

    我試圖重寫查詢,但HQL似乎並不正確地支持子查詢。我最新的嘗試是這樣的:

    select new list(te) from " + TaskEntity.class.getName() + " te 
    inner join (
        select te2.objectUuid, te2.taskName, max(te2.createdTimestamp) 
        from " + TaskEntity.class.getName() + " te2 
        group by te2.objectUuid, te2.taskName 
    ) teSub on 
        teSub.objectUuid = te.objectUuid and teSub.taskName = te.taskName 
    where 
        te.objectUuid = '" + domainObjectId + "' 
    

    但當然在「(下稱」連接語句

    因爲這是一個很常見的類型的查詢我不能相信以後有沒有辦法解決失敗時。與HQL + MSSQL工作。

    回答

    0

    嗯,哦,可這是一個錯字?

    max(teSub.createdTimestamp) 
    

    ,而不是

    max(te.createdTimestamp) 
    

    在子查詢中。

    相關問題