Java Projections.countDistinct与Hibernate一起产生意外结果
我有以下代码Java Projections.countDistinct与Hibernate一起产生意外结果,java,oracle,hibernate,criteria,Java,Oracle,Hibernate,Criteria,我有以下代码 Criteria criteria = this.getCriteriaForClass(DeviceListItem.class); Projection rowCountProjection = Projections.countDistinct("color"); criteria.setProjection(rowCountProjection); int rowCount = ((Long) criteria.uniqueResult()).intValue(); re
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。这是一个bug,在3.5.2中修复了: