【问题标题】:Count no of rows with group by clause in criteria计算条件中带有 group by 子句的行数
【发布时间】:2013-11-21 07:46:47
【问题描述】:

有没有办法为以下sql编写休眠条件:

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

基本上,我想从默认添加的 select 子句中排除 group 属性。

例子:

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

将导致

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

谢谢。

【问题讨论】:

标签: sql hibernate properties hibernate-criteria


【解决方案1】:

参考以下代码::

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

你也可以试试这个:::

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

【讨论】:

  • 可以通过HQL来完成。我只是想知道它是否可以通过标准来完成。谢谢。
猜你喜欢
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2016-01-03
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多