使用构造函数

时间:2017-03-15 09:46:04

标签: java hibernate hql

我试图执行以下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例外。当我删除" fetch"有用。然而,DossierAccount和Dossier之间的关联被指定为"懒惰"而且我不想改变它。但在这种特殊情况下,我需要热切地加载档案。

当我用变量" accountNumber"替换构造函数调用时,它也有效。但我需要在这里减少所选属性的数量。

有没有办法在select语句中使用嵌入式构造函数调用?

还有一个问题:我还需要限制已加入档案的选定属性数量。还有办法实现这个目标吗?

1 个答案:

答案 0 :(得分: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;
}

然而,这会将整个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:ConstructorNode不支持getDataType()!"例外