代码之家  ›  专栏  ›  技术社区  ›  frictionlesspulley sameer

Hibernate条件:使用group by子句投影计数

  •  3
  • frictionlesspulley sameer  · 技术社区  · 14 年前

    select count(*) as myCount from user group by name;
    

    我提出了以下标准

    DetachedCriteria.ForClass(typeof(UserDTO))
        .setProjections(Projections.ProjectionList()
                            .Add(Projections.rowCount(),"myCount")
                            .Add(Projections.groupProperty("this.name"));
    

    2 回复  |  直到 14 年前
        1
  •  0
  •   Diego Mijelshon    14 年前

    我不认为你能用标准来做,但是用HQL很容易。它与SQL查询的字符串完全相同,但使用实体/属性名称而不是表/列名称。

        2
  •  0
  •   victorskl    10 年前

    如果只有一个按列分组,则可以使用count distinct。

    HQL公司:

    select count(distinct name) as myCount from user
    

    DetachedCriteria.ForClass(typeof(UserDTO))
    .setProjections(Projections.ProjectionList()
                        .Add(Projections.countDistinct("name"),"myCount"));