tenwit wrote:
Count is a projection. Projections.count(prop) is what you need. If you want to filter your query based on counts, then you'll need to use DetachedCriteria to wrap the projection into an subquery. Something like
Code:
Criteria crit = ...;
DetachedCritieria countSubquery = DetachedCriteria.forClass(clazz);
countSubquery.setProjection(Projections.count("propToCount"));
crit.add(Subqueries.gt(Integer.valueOf(5), countSubquery));
return crit.list();
That'll get all instances of clazz that have the same "propToCount" as at most 4 other instances. (5 > (select ...))
Is there any way to accomplish this in version 2?
Would you use these same idioms to handle a counting sub-select within the FROM clause?
e.g.
SELECT a.infoA, b.infoB, c.itemCount
FROM TableA a, TableB b,
(SELECT count(*) FROM TableC WHERE c.infoA_ID) c
WHERE 1=1
AND a.infoA_ID = :qinfoA_ID
AND a.infoA_ID = b.infoA_FK(+)
From what I can tell, there is no way to accomplish either of these in V2 unless you run native SQL through SQLQuery. Am I wrong?