getNamedQuery不检索数据

时间:2013-11-21 07:18:51

标签: java sql hibernate

我使用org.hibernate.Session.getNamedQuery从xml文件中获取以下查询:

<sql-query name="filterTraineesBySection"><![CDATA[ 
   select usr.USERNAME, crs.NAME
 from TBLCOURSEROLE crsr
 inner join TBLCOURSE crs on crs.ID=:courseId and crsr.ROLEID=3
 inner join USER_ usr on crsr.USERID=usr.ID
]]>
</sql-query> 

我使用了以下方法:

    public ArrayList<String[]> getTraineesBySectionId(String sectionId) {
    String[] traineeDetails = null;
    ArrayList<String[]> traineesList = new ArrayList<String[]>();
    Query SgTrainees = null;
    SgTrainees = this.getSession().getNamedQuery("filterTraineesBySection");
    SgTrainees.setString("courseId", sectionId);
    Iterator trainees = SgTrainees.list().iterator();

    while (trainees.hasNext()) {
        Object[] tuple = (Object[]) trainees.next();
        traineeDetails = new String[2];
        traineeDetails[0] = (String) tuple[0];
        traineeDetails[1] = (String) tuple[1];
        traineesList.add(traineeDetails);;
    }


    return traineesList;

}

问题是上面的查询在DBMS中成功执行并检索了108行,而在java中它没有返回任何内容!

1 个答案:

答案 0 :(得分:0)

我认为[我不确定]您的查询是错误的。在第4行设置参数时需要空格

示例:

crs.ID  <SPACE HERE>  =:courseId 

完整格式见下文:

inner join TBLCOURSE crs on crs.ID =:courseId and crsr.ROLEID=3

<强>更新

你还需要setParameter喜欢以下内容:

SgTrainees.setParameter("courseId", sectionId); 

而不是SgTrainees.setString("courseId", sectionId);