0
"select c.type, c.date, sum(c.amount) from CustomerPayment c  where c.date  like '%" + year + "' and c.type='Cash'"

Above hibernate query gives ClassCastError because of my select statement.

How can i write it in proper way? Do need to use criteria for summation? What is the y good for above query? I'm confused.

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 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)

I get these error because of above query

0

2 Answers 2

1

you are selecting three properties: c.type, c.date and sum(c.amount), this means each result will be a Object[], containing each of the three selected properties. You can't cast this to CustomerPayment.

If CustomerPayment has a compatible constructor, you could do something like

select new CustomerPayment(c.type, c.date and sum(c.amount)) ...

or you could potentially do something like

"select c, c.type, c.date, sum(c.amount) from CustomerPayment c ... "

Object[] result = query.uniqueResult();
CustomerPayment payment = (CustomerPayment) result[0];

But, then I'm not sure what you need the sum for though, you aren't using the sum anywhere in the where clause. Its a bit unclear what you are trying to accomplish. As in, what do you want the result of the uniqueResult to be? A CustomerPayment instance? or a list of values?

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

4 Comments

but i have to select all them to get the proper result
please try adding the code where you execute the query, and access the result, via .list() or .uniqueResult() :)
i used uniqueResult() but it still causes problem
your problem is that you cant cast Object[] to CustomerPayment
0

Actually it won't return Object[] as per another answer. It will return Object[][]. Each array of it will consist of an array having 3 entries for c.type, c.date & sum(c.amount).

Refer the following diagram for easy understanding:

-----------------------------------
      |    0    |    1    |   2   |
-----------------------------------
  0   | c.type1 | c.date1 |  sum  |
-----------------------------------
  1   | c.type2 | c.date2 |  sum  |
-----------------------------------
  2   | c.type3 | c.date3 |  sum  |
-----------------------------------

Let me know the exact requirement of yours so that I can suggest you suitable solution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.