Projections.countDistinct with Hibernate produces unexpected result

北慕城南 提交于 2019-12-10 14:23:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/4616607/projections-countdistinct-with-hibernate-produces-unexpected-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!