获取java.lang.ClassCastException:org.hibernate.criterion.PropertyProjection错误

时间:2014-01-26 04:50:56

标签: hibernate hql criteria

我想用Hibernate Criteria实现以下SQL查询:

select abc_id, count(*) from boa_rep_deed where sync_system = ? And hook_id = ? group by abc_id order by abc_id

我试过的是这个...

List<teedObject> hytlow = null;
        Criteria criteria = session.createCriteria(teedObject.class);
        criteria.add(Restrictions.ne("abcID ", abcID));
        criteria.add(Restrictions.eq("syncSystem", syncSystem));
        criteria.add(Restrictions.eq("hookId", hookId));

        hytlow = criteria.list();

现在我的问题是我也有相应的pojo,如下所示..

class teedObject
{

    private long abcID ;
    private String syncSystem ;
    private String hookId;

    //and consisits other properties and setters and getters

}

现在我希望我的条件只获取某些列以使对象更轻,因此我必须使用条件中的投影,你能否告诉我如何在我的情况下使用投影以获取仅某些coulmns,我试过这个..

Criteria cr = session.createCriteria(teedObject.class);
cr.add(Restrictions.eq("syncSystem", syncSystem));
cr.add(Restrictions.eq("hookId", hookId));
cr.addOrder(Order.asc("abcID"));
cr.add(Projections.groupProperty("abcID")));
cr.setProjection(Projections.rowCount());
hytlow = cr.list();

但它正在抛出异常...... java.lang.ClassCastException: org.hibernate.criterion.PropertyProjection ..如果我以正确的方式实施预测,请指点一下

1 个答案:

答案 0 :(得分:0)

        List result = session.createCriteria(teedObject.class)       
                .add(Restrictions.eq("syncSystem", syncSystem))
                .add(Restrictions.eq("hookId", hookId))
                .setProjection(Projections.projectionList()
                        .add(Projections.groupProperty("abcID"))
                        .add(Projections.count("abcID"))           
                ).list();

请参阅Hibernate Group by Criteria Object