Sleep Mode Criteria: Calculate Forecast Using Group Offer

I want to execute the following SQL

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

I came up with the following criteria for the same

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

I get the result back as a pair of counter and name. How can I get only an account from this.

+3
source share
2 answers

I do not think you can do this using criteria, but it is easy using HQL. This is exactly the same row as your SQL query, but with object / property names instead of table / columns.

0
source

You can use a counter if there is only one group per column.

HQL:

select count(distinct name) as myCount from user

Criteria:

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

Source: https://habr.com/ru/post/1769245/


All Articles