使用休眠标准,并使用下一个表:
user category subcategory
A 1 1
A 1 2
B 1 1
B 2 1
¿获取具有max category和max子类别的用户的最佳方法是什么?
标准必须返回:
A 1 2
B 2 1
答案 0 :(得分:0)
最后,我用每个列的两个subCriterias解决了这个问题:
Criteria criteria = getSession().createCriteria(User.class, "c");
DetachedCriteria subCrit1 = DetachedCriteria.forClass(Participante.class, "s1")
.add(Restrictions.eqProperty("s1.user", "c.user"))
.setProjection(Projections.projectionList().add(Projections.max("s1.category")));
criteria.add(Subqueries.propertyEq("c.category", subCrit1 ));
DetachedCriteria subCrit2 = DetachedCriteria.forClass(User.class, "s2")
.add(Restrictions.eqProperty("s2.user", "c.user"))
.add(Restrictions.eqProperty("s2.category", "c.category"))
.setProjection(Projections.projectionList().add(Projections.max("s2.subcategory")));
criteria.add(Subqueries.propertyEq("c.subcategory", subCrit2));