2017-03-15 68 views
-2

我試圖執行以下HQL語句獲取:Hibernate的HQL加入與構造

 queryString = "select new DossierAccount(dossier,dossierAccount.accountNumber) from DossierAccount as dossierAccount join fetch dossierAccount.dossier as dossier" + 
        " where dossier.kycId.id != :kycId and dossierAccount.accountNumber in :accountNumbers"; 

這導致org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list例外。當我刪除「獲取」它的作品。然而,DossierAccount和Dossier之間的關聯被指定爲「懶惰」,我不想改變它。但在這種特殊情況下,我需要急切地加載檔案。

它也適用,當我用變量「accountNumber」替換構造函數調用。但是我需要減少這裏所選屬性的數量。

有沒有辦法在select語句中使用嵌入式構造函數調用?

還有一個問題:我還需要限制加入的Dossier的選定屬性的數量。有沒有辦法做到這一點?

+0

當你刪除'fetch'並保持關聯'LAZY'時會發生什麼? – Tobb

+0

相關檔案未加載,但我需要它在這種情況下 – thmayr

+0

什麼是未加載的意思,是否沒有提供給DossierAccount的值? – Tobb

回答

0

現在我更進一步了。下面的查詢選擇至少卷宗,沒有宣佈DossierAccount和檔案渴望之間的關聯:

 queryString = "select new DossierAccount((select dossier from Dossier as dossier where dossier.dossierId=dossierAccount.dossier.dossierId),dossierAccount.accountNumber) from DossierAccount as dossierAccount" + 
        " where dossierAccount.dossier.kycId.id != :kycId and dossierAccount.accountNumber in :accountNumbers"; 

爲此下面的構造是必需的:

public DossierAccount(Dossier dossier, AccountNumber accountNumber) { 
    this.accountNumber = accountNumber; 
    this.dossier  = dossier; 
} 

但是這個加載整個檔案內存中的數據,我不需要。於是,我選擇有限的一組檔案的屬性用下面的語句:

 queryString = "select new DossierAccount((select new Dossier(dossier.kycId,dossier.relationshipName) from Dossier as dossier where dossier.dossierId=dossierAccount.dossier.dossierId),dossierAccount.accountNumber) from DossierAccount as dossierAccount" + 
        " where dossierAccount.dossier.kycId.id != :kycId and dossierAccount.accountNumber in :accountNumbers"; 

但是,這導致了「ava.lang.UnsupportedOperationException:getDataType()不支持ConstructorNode」例外