2014-04-01 47 views
0

下面是函数和查询错误HQL查询像

public List<T> searchWordRecords(String parameter, String paramValue, Long statusID) throws AmmachiServerException 
    { 
     Session session = null; 
     List<T> recordList = null; 

     try 
     { 
      session = HibernateUtils.getHibernateConnection(); 
      String hqlQueryString = "Select p from " + theClass.getSimpleName() + " p where " + parameter +" like '% :paramValue%'and StatusID = :statusID"; 


      Query hqlQuery = session.createQuery(hqlQueryString); 
      hqlQuery.setParameter("paramValue", paramValue); 
      hqlQuery.setParameter("statusID", statusID); 
      recordList = hqlQuery.list(); 
     } 
     catch(HibernateException he) 
     { 
      processException(he); 
     } 
     finally 
     { 
      HibernateUtils.closeConnection(session); 
     } 

     return recordList; 
    } 

我得到了错误org.hibernate.QueryParameterException:无法找到名为参数[paramValue]

能否请你帮助别人解决这

回答

0

您可以创建参数反过来 - 通过通配符与它:

hqlQuery.setParameter("paramValue", "%" + paramValue + "%"); 

当然,你将不得不从当前查询中删除通配符:

where parameter like :paramValue 
+0

字符串hqlQueryString = “select p from”+ theClass.getSimpleName()+“p where”+ parameter +“=:paramValue and StatusID =:statusID”; hqlQuery.setParameter(“paramValue”,“%”+ paramValue +“%”);这是好的 – VSN

+0

你仍然需要匹配,所以继续使用''''而不是'=' – kostja

0

,如果你要搜索where foo like '%BAR%',而不是使查询where foo like '%:bar%'并设置:bar"BAR",你应该有你的查询作为where foo like :bar并设置:bar"%BAR%"

对于你的榜样,你应该:

String hqlQueryString = "Select p from " + theClass.getSimpleName() 
          + " p where " + parameter +" like :paramValue " 
          + " and StatusID = :statusID"; 
//.... 
hqlQuery.setParameter("paramValue", "%" + paramValue + "%"); 
//.... 

只是一个侧面说明。通常不推荐使用like,并将其与%value%匹配为通配符,因为字符串的开始将使大多数(如果不是全部的话)索引无用。