Hibernate的Projections.countDistinct产生意外结果 [英] Projections.countDistinct with Hibernate produces unexpected result
本文介绍了Hibernate的Projections.countDistinct产生意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码
Criteria criteria = this.getCriteriaForClass(DeviceListItem.class);
Projection rowCountProjection = Projections.countDistinct("color");
criteria.setProjection(rowCountProjection);
int rowCount = ((Long) criteria.uniqueResult()).intValue();
return rowCount;
,其目的是为名为"color"的字段找出具有不同值的行数.问题是
, 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");
返回与
Projections.count("color");
即使在数据库视图中有多个具有相同颜色的行.将Criteria对象转换为SQL时,我看到Hibernate生成的SQL是
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
我希望是
select count(distinct this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1
为什么它不能按预期工作,并且有一些补救措施?不幸的是,在这种情况下,我没有选择使用HQL.
Why doesn't it work like expected and is there some remedy? Unfortunately I have no option to use HQL in this case.
推荐答案
这是一个错误,已在3.5.2中修复: HHH-4957 .
It's a bug, fixed in 3.5.2: HHH-4957.
这篇关于Hibernate的Projections.countDistinct产生意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文