简体   繁体   English

Hql查询绑定数据成员

[英]Hql Query to Bind a Data Member

Query qry = session.createQuery("From RegistrationBean where ? = ?");
qry.setString(0,searchCriteria);
qry.setString(1,searchField);
searchList =(ArrayList<RegistrationBean>) qry.list();

RegistrationBean Entity class has userName, address, age fields.. I want to search a user by search criteria such as userName, address etc. using the above single query... But the query is returning me zero results even though the user exist.. RegistrationBean Entity类具有userName, address, age字段。.我想使用上述单个查询通过诸如userName, address等搜索条件搜索用户...但是,即使该用户存在,该查询仍向我返回零结果。 。

what's the problem? 有什么问题?

Both parameters are set to 0 position, the second is not set. 两个参数均设置为0位置,第二个参数未设置。 The position parameter should be set sequentially. 位置参数应顺序设置。

qry.setParameter(0,searchCriteria);
qry.setParameter(1,searchField);

But the field name should pass in the following way 但是字段名称应通过以下方式传递

String queryString = "from RegistrationBean as model where model." + propertyName   + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();

Try in the following way: 请尝试以下方式:

Query qry = session.createQuery("select * from RegistrationBean where :searchCrit = :searchValue");
qry.setString(":searchCrit",searchCriteria);
qry.setString(":searchValue",searchField);
searchList =(ArrayList<RegistrationBean>) qry.list();

It is more appropriate to use it like this instead of setting indexes (your problem is that you set both to 0), because if you change something in your query you might need to change your setters afterwards. 像这样使用它而不是设置索引更合适(您的问题是将两个都设置为0),因为如果您在查询中进行了某些更改,则可能需要在以后更改设置器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM