2016-06-14 62 views
0

所以我想用HQL做一個Select語句。HQL選擇一個OR

的選擇看起來是這樣的:

 queryParams[1] = new QueryParameter("clientObj", client); // Use Client as obj in hql 
     queryParams[0] = new QueryParameter("clientIDObj", client.ID); 

     if (!includeClosedSubTasks) 
     { 
      // tasks that aren't completed/closed/cancelled 
      qryString = "select st 
         from SubTask st 
         where st.ParentTask.ParentProject.ParentClient=:clientObj 
         and st.ParentTaskCategory.VisibleToClient=1 " 
         + filter + // more ands 
         "and st.ParentTaskStatus.ID != 3 
         and st.ParentTaskStatus.ID != 4 and st.ParentTaskStatus.ID != 8 
         or st.RaisedByClientID=:clientIDObj 
         order by st.CreateDateTime"; 
     } 

簡單來說,它應該這樣做:

選擇(A和B和C)或(d)ORDER BY .....

然而,當我執行此代碼,我收到了查詢語法錯誤與此腸子例外:

{「索引超出範圍必須爲非negat。並且小於集合的大小。\ r \ n參數名稱:index「}

我該如何解決這個問題?

回答

0

我認爲問題不在於查詢,而是在於queryParams數組。您不能添加空列表項是這樣的:

queryParams[1] = new QueryParameter("clientObj", client); // Use Client as obj in hql 

取而代之的是,總是試圖用列表的方法。新增。

嘗試將整個街區改寫爲這樣的:

if (!includeClosedSubTasks) 
{ 
    // TODO get session here somehow as you do normally 
    var session = NHSession.GetCurrent(); 

    // tasks that aren't completed/closed/cancelled 
    var qryString = @"SELECT st 
        FROM SubTask st 
        WHERE st.ParentTask.ParentProject.ParentClient = :clientObj 
        AND st.ParentTaskCategory.VisibleToClient = 1 " 
        + filter + // more ands 
        @" AND st.ParentTaskStatus.ID NOT IN (3, 4, 8) 
        OR st.RaisedByClientID = :clientIDObj 
        ORDER BY st.CreateDateTime"; 

    var query = session.CreateSQLQuery(qryString) 
     .SetParameter("clientObj", client) 
     .SetParameter("clientIDObj", client.ID); 

    // return query or whatever you need to do 
}