“where”限制不适用于带有join子句的hql查询

时间:2017-02-02 15:20:20

标签: sql hibernate join hql

首先澄清我对数据库非常不好,所以请不要吝啬我的代码:P

我在使用连接和限制的hibernate查询时遇到问题。我有一个很大的Assignments列表,其中一些有Asr个对象。

queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
    + " assignment.id, assignment.contract.assignmentStatus,"
    + " assignment.contract.beginDate, assignment.contract.endDate,"
    + " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
    + " assignment.organizationalData.hostCountryKey," 
    + " assignment.organizationalData.homeOrgUnitKey,"
    + " assignment.associate.globalIdAssociate,"
    + " assignment.associate.localIdHome,"
    + " assignment.associate.firstName,"
    + " assignment.associate.lastName)"
    + " from Assignment assignment left join assignment.asr asr"
    + " where assignment.contract.assignmentStatus.code = 5"
    + " and asr.homeAsrElegibility is not 'X'"
    + " or asr.homeAsrElegibility is null"
);

我正在逐步创建此查询。在我创建它之前没有使用join子句left join assignment.asr asr并且它运行良好但当然没有显示没有Assignments对象的Asr。在我添加了join子句之后,现在它显示每个Assignment(当assignmentStatus = 5的那些只有4.000时有10.000条记录)和

之类的限制
where assignment.contract.assignmentStatus.code = 5

不再反映在结果中。

总而言之:我需要一个包含所有赋值的列表,其赋值为assignmentStatus = 5和asr.homeAsrElegibility!='X'。但是,即使它们没有Asr对象,它也需要包含具有assignmentStatus = 5的所有赋值。

任何想法?谢谢!

1 个答案:

答案 0 :(得分:1)

括号有助于澄清情况。

queryString.append("select new commons.bo.assignment.AssignmentAssociateExtract("
    + " assignment.id, assignment.contract.assignmentStatus,"
    + " assignment.contract.beginDate, assignment.contract.endDate,"
    + " assignment.contract.contractType, assignment.organizationalData.homeCountryKey,"
    + " assignment.organizationalData.hostCountryKey," 
    + " assignment.organizationalData.homeOrgUnitKey,"
    + " assignment.associate.globalIdAssociate,"
    + " assignment.associate.localIdHome,"
    + " assignment.associate.firstName,"
    + " assignment.associate.lastName)"
    + " from Assignment assignment left join assignment.asr asr"
    + " where assignment.contract.assignmentStatus.code = 5"
    + " and ((asr.homeAsrElegibility is not null and asr.homeAsrElegibility is not 'X')"
    + " or (asr.homeAsrElegibility is null))"
);