2013-12-16 36 views
0

DaoLayer代碼是:拋出:IllegalArgumentException:在查詢中沒有位置參數

String query="from hibernate.EmpRecord u where u.uname=? and u.upass=?"; 

      Query hql =session.createQuery(query); 
      hql.setString(0, uname); 
      hql.setString(1, upass); 
       List<EmpRecord> userObjs= hql.list(); 
      if(userObjs.size() != 0) { 
        System.out.println(" User name : " + userObjs.get(0).getuname() + ", Status : " + userObjs.get(0).getIs_admin()); 
        userStatus = true; 

      } 
      transaction.commit(); 
      System.out.println(userStatus); 
    } catch(Exception e) { 
      e.printStackTrace(); 
      transaction.rollback(); 
    } 
    return userStatus; 


} 

堆棧跟蹤是:

inside Process Method 
Inside into service class 
Inside into dao class 
java.lang.IllegalArgumentException: No positional parameters in query: from hibernate.EmpRecord u where u.uname=? and u.upass=? 
    at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:356) 
    at org.hibernate.impl.AbstractQueryImpl.setString(AbstractQueryImpl.java:481) 
    at hibernate.EmpDao.verifyUserDaoAndPassDao(EmpDao.java:32) 
    at service.Empservice.verifyUserNameAndPassword(Empservice.java:20) 
    at web.LogController.processCredentials(LogController.java:24) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) 
    at java.lang.Thread.run(Unknown Source) 
+0

也許嘗試在查詢中使用空格= u.uname =?和u.upass =? –

回答

0

我有同樣的問題,我的壽查詢和他們的參數,其中,語法正確。在我的情況下,休眠拋出這個錯誤,因爲我忘了添加映射到hibernate.cfg.xml(或任何一種你正在使用的映射配置的)那樣:

<hibernate-configuration> 
    <session-factory> 
     ... 
     <mapping resource="com/hibernate/myproject/MyClassMapping.hbm.xml" /> 
     ... 
    </session-factory> 
</hibernate-configuration> 

添加映射後,一切運行良好。

在這種情況下,這是Hibernate引發的一個非常誤導性的錯誤。

0

檢查佔位符和參數的數量有幫助。

我在修改查詢時遇到了這個問題。之前,我的查詢有2個參數,更改後只有1個參數,我忘記從參數列表中刪除參數。

相關問題