【问题标题】:Projections.countDistinct with Hibernate produces unexpected resultProjections.countDistinct 与 Hibernate 产生意想不到的结果
【发布时间】:2011-06-04 17:38:24
【问题描述】:

我有以下代码


Criteria criteria = this.getCriteriaForClass(DeviceListItem.class);
Projection rowCountProjection = Projections.countDistinct("color");
criteria.setProjection(rowCountProjection);
int rowCount = ((Long) criteria.uniqueResult()).intValue();
return rowCount;

,其目的是找出名为“color”的字段具有不同值的行数。问题是


Projections.countDistinct("color");

返回与

相同数量的结果

Projections.count("color");

即使在数据库视图中有多个具有相同颜色的行。将Criteria对象转换为SQL时,看到Hibernate产生的SQL是


select count(this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1

当我期望它是


select count(distinct this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1

为什么它没有像预期的那样工作,有什么补救措施吗?不幸的是,在这种情况下我没有选择使用 HQL。

【问题讨论】:

    标签: java oracle hibernate criteria


    【解决方案1】:

    这是一个错误,已在 3.5.2 中修复:HHH-4957

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多