本文介绍了休眠查询-ClassCastError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

由于我的select语句,上面的休眠查询给出了ClassCastError.

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

推荐答案

您正在选择三个属性:c.typec.datesum(c.amount),这意味着每个结果将是一个Object [],其中包含每个三个选定的属性.您不能将其投射到CustomerPayment.

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.

如果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];

但是,但是我不确定您需要什么总和,因为您没有在where子句中的任何地方使用总和.尚不清楚您要完成什么.在这种情况下,您希望uniqueResult的结果是什么?一个CustomerPayment实例?或值列表?

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?

这篇关于休眠查询-ClassCastError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 00:42
查看更多