【问题标题】:ClassCastException when i query查询时出现 ClassCastException
【发布时间】:2013-05-01 22:09:03
【问题描述】:

我有以下代码:

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)

可能是什么原因?为什么我会收到此错误?是否有由于浮动返回而导致该错误的原因?可能是什么原因?为什么我会收到此错误?可能是什么原因?为什么我会收到此错误?

【问题讨论】:

  • 哪一行给出了错误?
  • 尝试从您的查询中删除select c.type, c.date, sum(c.amount) 。最好使用命名参数并让 hibernate 为您设置参数。
  • 所以如果我删除它们,我该如何合计金额列?

标签: java hibernate


【解决方案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.

【讨论】:

  • 所以如果我删除它们,我该如何合计金额列?
  • 你期望 sum 列的值去哪里?
  • 我需要合计金额列
  • @user2341009 你的回复没有回答我的问题。
  • 我需要把总和放到我的 CustomerPayment.amount 中?
【解决方案2】:

您选择的是 CustomerPayment.type、CustomerPayment.date、sum(CustomerPayment.amount),而不是 CustomerPayment 对象

【讨论】:

  • 所以如果我删除它们,我该如何合计金额列?
  • 您不必删除它,只是不能将其投射到 CustomerPayment。它可能返回大小为 3 的数组。
  • 那么正确的方法是什么?我需要选择什么吗?
  • 获取一本书 user2341009 或通过教程这是非常基本的
猜你喜欢
  • 1970-01-01
  • 2013-04-21
  • 2018-08-25
  • 2012-01-06
  • 2017-09-15
  • 1970-01-01
  • 2015-07-15
  • 2011-05-23
  • 2019-01-10
相关资源
最近更新 更多