Java org.hibernate.QueryException:并非所有命名参数都已设置:[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18160394/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:55:22  来源:igfitidea点击:

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

javahibernatejpa

提问by user2670113

I'm getting extremely strange behavior out of JPA 2.0

我从 JPA 2.0 中得到了非常奇怪的行为

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

我正在尝试构建一个看起来像这样的查询,其中 employeeId 和 empDepartment 是通过 java 参数传递的长值

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?

但是上面的查询第一次不起作用,它产生了上面的错误,但是当我第二次再次触发相同的方法时,一切都很顺利,每次都发生这种情况,可能是什么原因?

回答by Dennis Kriechel

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.

如果这也不起作用,那么您的查询内容也可能有问题,而不是替换参数。也许类型不正确。参数类型由上下文推断。

回答by Adelin

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.

我不知道是什么原因,但它发生在我身上一次。像这样试试。只需删除 =:empId 和其他参数之间的空格。