2011-01-06 43 views
5

我有以下代码Projections.countDistinct与Hibernate会产生意想不到的结果


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

,其目的是找出与一个名为“色”的领域不同值的行数。问题是,


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。

回答

相关问题