Hibernate Groupby总和

时间:2018-03-23 09:57:58

标签: java hibernate

我想在hibernate中得到groupby sum。但是有一个名为" java.lang.ClassCastException的错误:[Ljava.lang.Object;不能被投射到"以及如何解决这个问题?

这就是我想要的

select sum(this_.actual_qty), this_.c_code from m_crop_projections this_ where this_.cc_code='1089' and this_.entered_date='2018-03-22' group by this_.c_code

休眠

@Override
public List<CropProjections> doInHibernate(Session session) throws HibernateException {
    System.out.println("inside repo-----"+colCode);
    System.out.println("inside repo-----"+date);
    Date convert_date = getUtilDate(date);

    Criteria criteria = session.createCriteria(CropProjections.class, "cp");

    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.alias(Projections.sum("actualqty"),"cp.actualqty"));
    projList.add(Projections.groupProperty("cropcode"));


    criteria.add(Restrictions.eq("colcode", colCode));
    criteria.add(Restrictions.eq("entereddate", convert_date));
    criteria.setProjection(projList);
    return criteria.list();
}

服务类

@Override
@Transactional
public List<CropProjectionsDTO> getTotalReadyQty(String colCode, String date) {
    List<CropProjectionsDTO> list = new ArrayList<CropProjectionsDTO>();
    List<CropProjections> cropPrcojections = cropProjectionsDAO.getTotalReadyQty(colCode,date);     

    System.out.println(cropPrcojections);

    if (cropPrcojections.isEmpty()) {
        System.out.println("cropPrcojections Data Null-------");
    } else {
        System.out.println("cropPrcojections Data not-Null-------");
        for (CropProjections cropPro : cropPrcojections) {

            CropProjectionsDTO dto = new CropProjectionsDTO();
            dto.setActualqty(cropPro.getActualqty());
            dto.setCropcode(cropPro.getCropcode());

            System.out.println(cropPro.getActualqty());
            System.out.println(cropPro.getCropcode());
            list.add(dto);
        }
    }

    return list;
}

在Service类中的for循环中发生错误。但是,cropPrcojections具有通过hibernate query解析的值。错误是cropPrcojections无法转换为cropPro ..如何解决这个问题?

0 个答案:

没有答案