'org.hibernate.QueryException: Not all named parameters have been set:[]

I'm getting extremely strange behavior out of JPA 2.0

I'm trying to build a query that looks likes, where employeId and empDepartment are long values passing through java arguments

Query query=em.createQuery("SELECT e FROM Employee e WHERE e.empId = :empId and e.empDepartment = :empDepartment");
query.setParameter("empId" ,employeId);
query.setParameter("empDepartment",empDepartment);

But the above query doesnt work for first time, it generating the above error but when second time i trigged the same method again every thing went smoothly, this happens each and every time, what could be the reason for it?



Solution 1:[1]

You could try this:

Query query=em.createQuery("SELECT e FROM Employee e WHERE e.empId = ? and  e.empDepartment = ?");
query.setParameter(1, employeId);
query.setParameter(2, empDepartment);

If this is not working either, than there can be a problem with your query content as well, not with the replacement of the parameters. Maybe the the types are not correct. The parameter type is inferred by the context.

Solution 2:[2]

Query query=em.createQuery("SELECT e FROM Employee e WHERE e.empId=:empId and e.empDepartment=:empDepartment");

query.setParameter("empId" ,employeId);
query.setParameter("empDepartment",empDepartment);

I don't know what was the reason, but it happened to me once. Try it like this. Just remove white spaces between =:empId and the other parameter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 Adelin