Reputation: 201
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.
This is my hql query
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.
Upvotes: 1
Views: 13772
Reputation: 3056
You forget to set parameter for named parameters : change dao method like:
public List getByIsActive(boolean isActive) {
return getSession().createQuery(
"from User where isActive = :isActive").setParameter("isActive", isActive)
.list();
}
Upvotes: 3
Reputation: 509
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();
}
Upvotes: 0