Projectionsを調べたいと思うかもしれません。「タイプ」関連付けでProjections.groupProperty()を使用し、結果を合計できるはずです。この SO の質問を確認してください: Expressions in hibernate criteria
編集:試してみましたが、以下はうまくいくようです:
注文:
@Entity
@Table(name="orders")
public class Order {
@Id
@GeneratedValue
private long id;
@ManyToOne
private Type type;
}
タイプ:
@Entity
@Table(name="order_types")
public class Type {
@Id
@GeneratedValue
private long id;
}
および基準:
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]);
}