2011-01-06 44 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。

回答

相關問題