maxhuang
maxhuang

Reputation: 2961

Count no of rows with group by clause in criteria

is there a way to write hibernate criteria for the following sql:

select count(*) from tableA group by columnA, columnB, columnC;

Basically, I want to exclude the group property from the select clause which is added by default.

Example:

session.createCriteria(TableA.class)
  .setProjection(Projections.projectionList()
    .add(Projections.groupProperty("columnA"))
    .add(Projections.groupProperty("columnB"))
    .add(Projections.groupProperty("columnC"))
    .add(Projections.rowCount()));

will result in

select columnA, columnB, columnC, count(*) from tableA group by columnA, columnB, columnC;

Thanks.

Upvotes: 1

Views: 493

Answers (1)

dev
dev

Reputation: 715

refer below code::

Integer totalResult = ((Number)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();

You can also try this:::

int count = ((Long)getSession().createQuery("select count(*) from table_a group by column_a").uniqueResult()).intValue();

Upvotes: 1

Related Questions