java 引起:org.hibernate.QueryException:并非所有命名参数都已设置:[isActive] [from User where isActive = :isActive]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38971685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caused by: org.hibernate.QueryException: Not all named parameters have been set: [isActive] [from User where isActive = :isActive]
提问by Francis
I have a table of users made up of a boolean value of active users as shown in the following table
我有一个由活动用户的布尔值组成的用户表,如下表所示
id | name | active
1 | john | false
2 | bob | true
3 | jeff | true
On the above structure, I want to retrieve the list of users where isActive is equal to true.
在上面的结构中,我想检索 isActive 等于 true 的用户列表。
This is my hql query
这是我的 hql 查询
public List getByIsActive(boolean isActive) {
return getSession().createQuery(
"from User where isActive = :isActive").list();
}
I am retrieving it like this
我像这样检索它
@ResponseBody
@RequestMapping(value = "/get-all-activeusers", method = RequestMethod.GET)
public List<User> getAllActiveUsers() {
try {
boolean isActive = true;
return _userDao.getByIsActive(isActive);
} catch (Exception e) {
logger.error("Exception in fetching active users: ", e.getStackTrace());
e.printStackTrace();
}
return null;
}
And I am getting this error
我收到这个错误
Caused by: org.hibernate.QueryException: Not all named parameters have been set: [isActive] [from User where isActive = :isActive]
at org.hibernate.internal.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:401)
at org.hibernate.internal.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:385)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:99)
at com.models.UserDao.getByIsActive(UserDao.java:64)
Please where I getting it wrong in my attempt. I have researched but it is not tailored to the problem am facing.
请问我在尝试中哪里出错了。我已经研究过,但它不是针对我面临的问题量身定制的。
回答by Bhuwan Prasad Upadhyay
You forget to set parameter for named parameters : change dao method like:
您忘记为命名参数设置参数:更改 dao 方法,例如:
public List getByIsActive(boolean isActive) {
return getSession().createQuery(
"from User where isActive = :isActive").setParameter("isActive", isActive)
.list();
}
回答by vbuhlev
As the message says you are not setting a parameter which you are using in the query and it doesn't know what to do.
正如消息所说,您没有设置您在查询中使用的参数,它不知道该怎么做。
It should look like this:
它应该是这样的:
public List getByIsActive(boolean isActive) {
return getSession().createQuery("from User where isActive = :isActive")
.setParameter("isActive", isActive)
.list();
}