Hibernate无法找到命名参数异常

时间:2015-04-23 08:22:06

标签: hibernate spring-mvc

我的异常消息是:

org.hibernate.QueryParameterException: could not locate named parameter [searchKey] at
org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:148) at 
org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:165) at
org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:523) at
org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:493) at
com.si.bse.dao.impl.SearchDaoImpl.getClientList(SearchDaoImpl.java:99) at 
com.si.bse.services.impl.SearchServicesImpl.getClientList(SearchServicesImpl.java:32)

并且抛出它的代码是:

@SuppressWarnings("unchecked")
@Transactional
@Override
public List<Client> getClientList(String searchWord) {
    String sql = "select * from client c join clientcategory cc on c.id=cc.client_id where match(cc.clientkeyword) against(':searchKey' in boolean mode)";

    SQLQuery query = (SQLQuery) getCurrentSession().createSQLQuery(sql).addEntity(Client.class).setParameter("searchKey", searchWord);

    List result = query.list();
    return result;
}

为什么Hibernate找不到我的命名参数?

1 个答案:

答案 0 :(得分:0)

无需为参数占位符'添加单引号':searchKey'。删除引号可能会解决您的问题。

所以查询将是:

select * from client c join clientcategory cc on c.id=cc.client_id where match(cc.clientkeyword) against(:searchKey in boolean mode)

您可以使用重载的setParameter()指定参数的数据类型,该参数接受3个参数

  1. 参数名称
  2. 参数值
  3. Hibernate类型
  4. 有关方法Plz的更多信息,请参阅Hibernate doc