无法找到命名参数

时间:2015-01-30 12:18:26

标签: java hibernate hql

我无法理解为什么hibernate没有找到参数" setor"下面的查询。

hql.append("select top :limite * from MA3OCORT oco,MA4DETOT ocodA " +
           "    where MA4IDOCO=ma3idoco " +
           "    and ocodA.MA4IDODE in (select max(ocodB.MA4IDODE) from MA4DETOT ocodB where ocodA.MA4IDOCO=ocodB.MA4IDOCO and ocodB.MA4IDSIT=:situacao)" +
           "    and (oco.MA3DSSOL like :solicitante or :solicitante is null)" +
           "    and (ocodA.MA4DTDET between :datai and :dataf or :dataf is null)" +
           "    and ocodA.MA4IDSET = :setor" +
           "order by ocodA.MA4DTDET desc");

return em.createNativeQuery(hql.toString(), OcorrenciaDetalhe.class)
       .setParameter("situacao", situacao)
       .setParameter("solicitante",  "%" + solicitanteFiltro + "%")
       .setParameter("datai", dataRespostaFiltro1)
       .setParameter("dataf", dataRespostaFiltro2)
       .setParameter("setor", usuarioLogado.getSetor().getId())
       .setParameter("limite", limit).getResultList();

2 个答案:

答案 0 :(得分:2)

因为select

中存在语法错误

这个

"   and ocodA.MA4IDSET = :setor" +
"order by ocodA.MA4DTDET desc");

变为

'   and ocodA.MA4IDSET = :setororder by ocodA.MA4DTDET desc'

您需要在:setor之后或order之前添加空白字符。

答案 1 :(得分:0)

您必须通过以下方式在行业和订单之间建立一个空格:

像那样:

 hql.append("select top :limite * from MA3OCORT oco,MA4DETOT ocodA " +
       "    where MA4IDOCO=ma3idoco " +
       "    and ocodA.MA4IDODE in (select max(ocodB.MA4IDODE) from MA4DETOT ocodB where ocodA.MA4IDOCO=ocodB.MA4IDOCO and ocodB.MA4IDSIT=:situacao)" +
       "    and (oco.MA3DSSOL like :solicitante or :solicitante is null)" +
       "    and (ocodA.MA4DTDET between :datai and :dataf or :dataf is null)" +
       "    and ocodA.MA4IDSET = :setor" +
       "    order by ocodA.MA4DTDET desc");

 return em.createNativeQuery(hql.toString(), OcorrenciaDetalhe.class)
   .setParameter("situacao", situacao)
   .setParameter("solicitante",  "%" + solicitanteFiltro + "%")
   .setParameter("datai", dataRespostaFiltro1)
   .setParameter("dataf", dataRespostaFiltro2)
   .setParameter("setor", usuarioLogado.getSetor().getId())
   .setParameter("limite", limit).getResultList();