I have a table called Gift, which has a one-to-many relationship to a table called ClickThrough - which indicates how many times that particular Gift has been clicked. I nee
If you want to return a list of entities or properties in combination with a count you will need a group by. In criteria this is done through ProjectionList
and Projections
. e.g.
final ProjectionList props = Projections.projectionList();
props.add(Projections.groupProperty("giftID"));
props.add(Projections.groupProperty("giftName"));
props.add(Projections.count("giftID"), "count"); //create an alias for the count
crit.setProjection(props);
crit.add(Order.desc("count")); //use the count alias to order by
However, since you're using ProjectionList
you will also need to use AliasToBeanConstructorResultTransformer.