Double monthCounts = 0.000;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String month = sdf.format(new Date());
//通过截取订单时间的年月来统计月订单数
String hql = "select * from OrderInfo o where substring(o.orderTime,0,8) ='"+month+"'";
List<OrderInfo> orders = this.getSession().createSQLQuery(hql).list();      
for(OrderInfo order : orders){             //遍历月的所有订单
System.out.println("order对象::"+order);
monthCounts += order.getAllPrice();
}
return monthCounts;java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.lamp.model.OrderInfo
拿不到对象...转不过来!!!

解决方案 »

  1.   

    String hql = "select * from OrderInfo o where substring(o.orderTime,0,8) ='"+month+"'";--->String hql = "select o from OrderInfo o where substring(o.orderTime,0,8) ='"+month+"'";
      

  2.   

    List<OrderInfo> orders = this.getSession().createSQLQuery(hql).list();
    你这是根据sql语句来查的String hql="from OrderInfo o where substring(o.orderTime,0,8) ='"+month+"'"
    List<OrderInfo> orders = this.getSession().find(hql)
      

  3.   


    你的错误应该是在于this.getSession().createSQLQuery(hql).list();这里,这里应该是this.getSession().createSQLQuery(hql).。addEntity(OrderInfo.class).list();  
    此外你这是SQl查询不是hql查询语句噢
      

  4.   

    很明显啊,类型转换错误!查询list()返回的是object结果集,不能直接转换成OrderInfo数据集!需要强转,你可以返回类型定义成List,去掉后面的<OrderInfo>,在用的时候取出一个对象然后强转即可
      

  5.   


    个人觉得 一个个拿出来强制转换很麻烦…………hibernate中有方法可用啊  直接把所有的Object类型转换成OrderInfo再放到List中就可以了