0

i have below code:

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();

    }

However, it gives ClassCastException.

Stack trace:

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)

What might be the reason? Why i get this error? Is there a reason of that error because of float returning?What might be the reason? Why i get this error? What might be the reason? Why i get this error?

3
  • What line gives you the error? Commented May 1, 2013 at 22:11
  • Try removing select c.type, c.date, sum(c.amount) from your query. It's also better to use named params and let hibernate set the arguments for you. Commented May 1, 2013 at 22:11
  • so if i delete them how can i sum amount column? Commented May 1, 2013 at 22:20

2 Answers 2

1

You are selecting parts of the c instead of SELECTing a c object. Here are some examples of valid HQL. I think this will work the way you want:

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

But, that's not really great to do either because you can do a sql injection attack here. You should turn that year into a variable. Here are a bunch of examples of how to do that.

Sign up to request clarification or add additional context in comments.

6 Comments

so if i delete them how can i sum amount column?
Where do you expect the value of the sum column to go?
i need to sum amount column
@user2341009 Your reply did not answer my question.
i need to put summation to my CustomerPayment.amount ?
|
0

You are selecting CustomerPayment.type, CustomerPayment.date, sum(CustomerPayment.amount), not CustomerPayment object

4 Comments

so if i delete them how can i sum amount column?
You don't have to delete it, you just can't cast it to CustomerPayment. It probably returns array with size 3.
so what is the proper way to do that? Do i need to select what?
get a book user2341009 or go thru a tutorial this is pretty basic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.