Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Projections.countDistinct with Hibernate produces unexpected result

I have the following code


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

, whose purpose is to find out the number of rows with different values for the field named "color". The problem is that


Projections.countDistinct("color");

returns the same number of results as


Projections.count("color");

even though there are multiple rows with same color in the database view. When converting the Criteria object to SQL, I see that the SQL produced by Hibernate is


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

when I would expect it to be


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

Why doesn't it work like expected and is there some remedy? Unfortunately I have no option to use HQL in this case.

like image 671
simon Avatar asked Jan 06 '11 15:01

simon


1 Answers

It's a bug, fixed in 3.5.2: HHH-4957.

like image 97
axtavt Avatar answered Oct 20 '22 12:10

axtavt