2013-05-01 66 views
0

我有下面的代碼:ClassCastException異常,當我查詢

public float getTotalCash(String year) { 


     CustomerPayment cp = null; 

     this.session = HibernateUtil.getSessionFactory().getCurrentSession(); 


     try { 
      org.hibernate.Transaction tx = session.beginTransaction(); 
      Query q = session.createQuery("select c.type, c.date, sum(c.amount) from CustomerPayment c where c.date like '%" + year + "' and c.type='Cash'"); 
      cp = (CustomerPayment) q.uniqueResult(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return totalCashAmount = cp.getAmount(); 

    } 

然而,它給ClassCastException異常。

堆棧跟蹤:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to tekirmobile.CustomerPayment 
    at tekirmobile.clController.getTotalCash(clController.java:163) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:616) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:679) 

可能是什麼原因?爲什麼我得到這個錯誤?是否有浮動返回的錯誤原因?可能是什麼原因?爲什麼我得到這個錯誤?可能是什麼原因?爲什麼我得到這個錯誤?

+0

什麼線給你的錯誤? – 2013-05-01 22:11:36

+0

嘗試從查詢中刪除'select c.type,c.date,sum(c.amount)'。最好使用命名參數,讓hibernate爲你設置參數。 – 2013-05-01 22:11:56

+0

所以如果我刪除他們,我怎麼總結金額欄? – user2341009 2013-05-01 22:20:24

回答

1

您正在選擇部分c而不是選擇c對象。 Here are some examples of valid HQL.我認爲這會工作,你所希望的方式:

"from CustomerPayment c where c.date like '%" + year + "' and c.type='Cash'" 

但是,這不是真正偉大做是因爲你可以在這裏做一個SQL注入攻擊。你應該把這個year變成一個變量。 Here are a bunch of examples of how to do that.

+0

所以如果我刪除他們如何總結金額欄? – user2341009 2013-05-01 22:19:29

+0

你認爲總和欄的價值何去何從? – 2013-05-01 22:20:26

+0

我需要總金額欄 – user2341009 2013-05-01 22:20:51

0

您選擇CustomerPayment.type,CustomerPayment.date,SUM(CustomerPayment.amount),不CustomerPayment對象

+0

所以如果我刪除它們,我該如何總結金額欄? – user2341009 2013-05-01 22:19:59

+0

你不必刪除它,你只是無法將其轉換爲CustomerPayment。它可能返回大小爲3的數組。 – maszter 2013-05-01 22:22:49

+0

那麼有什麼方法可以做到這一點?我需要選擇什麼? – user2341009 2013-05-01 22:28:50