Java 参数不作为命名参数存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24322073/
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
Parameter does not exist as a named parameter
提问by AJ.
My query is like this:
我的查询是这样的:
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo =:contactNo");
query1.setInteger("contactNo", 22);
The error I got is:
我得到的错误是:
Exception in thread "main" java.lang.IllegalArgumentException: Parameter contactNo does not exist as a named parameter in [select c.email from Contact c where c.contactNo = ?]
线程“main”中的异常 java.lang.IllegalArgumentException:参数 contactNo 作为命名参数不存在 [select c.email from Contact c where c.contactNo = ?]
It is working fine with
它工作正常
Query query = session.createQuery("select c.email from Contact c where c.contactNo = ?");
query.setInteger(0, 22);
Why 1st query is not working?
为什么第一个查询不起作用?
回答by michikot
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo =:contactNo");
query.setParameter("contactNo", 22);
try using setParameter instead of setInteger
尝试使用 setParameter 而不是 setInteger
回答by RemyG
I think you may need a space before your parameter:
我认为您的参数前可能需要一个空格:
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo = :contactNo");
query.setInteger("contactNo", 22);