I have an entity whose id is a composite of several fields. I am trying to do a detached criteria query against several child records but have the result returned as the parent object. I read that the way to do this was to do a projection on the id, then use this id in a second query to get the distinct list of records. Normally one wouldn't have to do this but since we're wanting to restrict the records returned by paging to avoid unnecessary memory consumption, we do.
Code:
DetachedCriteria dc = DetachedCriteria.forClass(RequestForQuotation.class);
/* set the criteria restrictions here */
dc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
dc.setProjection(Projections.id());
Criteria criteria = session.createCriteria(RequestForQuotation.class);
criteria.add(Subqueries.propertyIn("id", dc));
criteria.setFirstRow((pageNumber-1)*pageSize).setMaxResults(pageSize);
return(criteria.list());
By using the above code I get the following error:
property does not map to a single column: idIronically if I move from detached criteria to regular criteria, this works, but the goal was not to perform two
list() calls to obtain the data which should be available via the Subqueries class.
Code:
Criteria dc = session.createCriteria(RequestForQuotation.class);
/* set the criteria restrictions here */
dc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
dc.setProjection(Projections.id());
List<RequestForQuotationId> ids = dc.list();
Criteria criteria = session.createCriteria(RequestForQuotation.class);
criteria.add(Restrictions.in("id", ids));
criteria.setFirstRow((pageNumber-1)*pageSize).setMaxResults(pageSize);
return(criteria.list());
Can anyone share how I could fix this?