在hibernate中运行子查询时出现意外的令牌错误

时间:2013-03-15 12:43:09

标签: java hibernate

我正在尝试在hibernate中运行子查询,但我正在

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

    }

由于

1 个答案:

答案 0 :(得分: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注入攻击。