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);

为什么我收到此错误?这是我的休眠查询

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();

    }

谢谢

4

1 回答 1

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 注入攻击。

于 2013-03-15T12:50:12.870 回答