DetachedCriteria - 分组依据

时间:2014-08-28 09:46:08

标签: java hibernate

我使用分离进行搜索,并且我使用分组依据。我的代码如下:

@Transactional(propagation = Propagation.REQUIRED)
@Override
public List<Dossier> rechercher(DossierBean critere)
throws TechnicalException {
    DetachedCriteria criteria = DetachedCriteria.forClass(Dossier.class);

    criteria.add(Restrictions.eq("firstName", critere.getFirstName()));
    criteria.add(Restrictions.eq("secondName", critere.getSecondName()));
    criteria.add((Criterion) Projections.groupProperty("iddossier"));
    List<Dossier> dList = findByCriteria(criteria);
    return dList;
}

我得到了这个例外:

Caused by: java.lang.ClassCastException: org.hibernate.criterion.PropertyProjection cannot
     be cast to org.hibernate.criterion.Criterion

我知道groupProperty(...)是问题,但我不能忽视我的小组,因为我会得到一条双线。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

要向DetachedCriteria添加投影,请使用setProjection()方法,如下例所示:

DetachedCriteria criteria = DetachedCriteria.forClass(Dossier.class);

criteria.add(Restrictions.eq("firstName", critere.getFirstName()));
criteria.add(Restrictions.eq("secondName", critere.getSecondName()));
criteria.setProjection(Projections.groupProperty("iddossier"));