you might wanna look into Projections. You should be able to use Projections.groupProperty() on the "type" association, and add up the results.
check this SO question: Expressions in hibernate criteria
edit: tried it and the following seems to work fine:
Order:
@Entity
@Table(name="orders")
public class Order {
@Id
@GeneratedValue
private long id;
@ManyToOne
private Type type;
}
Type:
@Entity
@Table(name="order_types")
public class Type {
@Id
@GeneratedValue
private long id;
}
and the Criteria:
Criteria c=sess.createCriteria(Order.class)
.createCriteria("type")
.setProjection(
Projections.projectionList()
.add(Projections.groupProperty("id"))
.add(Projections.rowCount(),"rowcount")
)
.addOrder(org.hibernate.criterion.Order.desc("rowcount"));
for (Object[] result:(List<Object[]>) c.list()){
System.out.println("type id: " + result[0] + "\tcount: " + result[1]);
}