【问题标题】:get max of two related colums of the same table with hibernate criteria使用休眠条件获取同一表的两个相关列的最大值
【发布时间】:2014-04-14 20:49:13
【问题描述】:

使用休眠条件,并使用下一个表:

user  category  subcategory
A     1         1
A     1         2
B     1         1
B     2         1

¿获取最大类别和最大子类别的用户的最佳方法是什么?

条件必须返回:

A     1         2
B     2         1

【问题讨论】:

    标签: hibernate greatest-n-per-group hibernate-criteria


    【解决方案1】:

    最后我用每列两个子标准解决了这个问题:

    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));
    

    【讨论】:

      猜你喜欢
      • 2014-12-17
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 2020-09-22
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多