Kadiri Kadiri -4 years ago 128
Java Question

DetachedCriteria - Group by

I do a search by using a detached, and I've to use a group by. My code is bellow :

@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;
}


I get this exception:

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


I know that
groupProperty(...)
is the problem, but I cannot ignore my group by because I'll get a double lines. Any suggestion?

Answer Source

To add projections to DetachedCriteria use setProjection() method for that as in example below:

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"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download