我的查询是这样的:
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo =:contactNo");
query1.setInteger("contactNo", 22);
我得到的错误是:
线程“main”中的异常java.lang.IllegalArgumentException: 参数contactNo不作为[select中的命名参数存在 来自Contact c的c.email,其中c.contactNo =?]
工作正常
Query query = session.createQuery("select c.email from Contact c where c.contactNo = ?");
query.setInteger(0, 22);
为什么第一个查询不起作用?
答案 0 :(得分:4)
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo =:contactNo");
query.setParameter("contactNo", 22);
尝试使用setParameter而不是setInteger
答案 1 :(得分:0)
Just Try this
**Step 1--:** Add How many parameter you need just add in hashmap
-------------------------------------------------------------------
HashMap param_List=new HashMap();
param_List.put("contactNo",22);
**Step 2--:** Just You pass your Query
-------------------------------------------------------------------
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo =:contactNo");
**Step 3--:** What ever Data type is no matters but get an output.
-------------------------------------------------------------------
for(Object paramKey : param_List.keySet())
{
query1.setParameter(paramkey.toString(), param_List.get(paramKey);
}
**Step 4--:**
-------------------------------------------------------------------
String finalResult=query1.getSingleResult().toString();
答案 2 :(得分:-1)
我认为你的参数可能需要一个空格:
Query query1 = session.createQuery("select c.email from Contact c where c.contactNo = :contactNo");
query.setInteger("contactNo", 22);