2013-03-15 40 views
0

我試圖運行在休眠子查詢運行子查詢,但我正在逐漸得到意外的標記錯誤,而在休眠

nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 
where near line 1, column 183 

先到這裏查詢的SQL

select * 
from OS_Historystep hs1 
where step_name = '011' 
and finish_date = (select max(finish_date) 
         from OS_Historystep hs2 
         where hs2.step_name = hs1.step_name 
         and hs2.ref_id = hs1.ref_id); 

爲什麼我收到這個錯誤?這裏是我的Hibernate查詢

StringBuffer query2 = new StringBuffer(); 
    query2.append(" from "); 
    query2.append(WflWorkflowHistoryStep.class.getName()); 
    query2.append(" hs1 where hs1.stepName = " + workFlowStepName); 
    query2.append(" and hs1.finishDate = (select max(hs2.finishDate) from " 
      + WflWorkflowHistoryStep.class.getName() + " hs2)"); 
    query2.append("where hs2.stepName = hs1.stepName and hs2.refId = hs1.refId"); 

    try { 

     historyStepList = getHibernateTemplate().find(query2.toString()); 

    } catch (Exception e) { 

     String message = e.getMessage(); 
     System.out.println(); 

    } 

感謝

回答

0

如果你寫你的查詢是這樣的:

from WflWorkflowHistoryStep hs1 
where hs1.stepName = ? 
    and hs1.finishDate=(select max(hs2.finishDate) from WflWorkflowHistoryStep hs2) 
                       ^
where hs2.stepName = hs1.stepName and hs2.refId = hs1.refId 
^^^^^ 

你看,你有兩個where子句。我想你可能打算把你的支架放在其他地方。

此外,使用參數(?)而不是連接字符串,否則你打開自己的HQL注入攻擊。

+0

謝謝:)查詢應該是'query2.append(「from」); \t \t query2.append(WflWorkflowHistoryStep.class.getName()); \t \t query2.append(「hs1 where hs1.stepName =?」); \t \t query2.append(」和hs1.finishDate =(選擇最大值(從hs2.finishDate) 「 \t \t \t \t + WflWorkflowHistoryStep.class.getName()+」 HS2 「); \t \t query2.append(」其中hs2.stepName = hs1.stepName和hs2.refId = hs1.refId)「);' – Basit 2013-03-19 08:29:13