sim*_*mon 5 java oracle hibernate criteria
我有以下代码
Criteria criteria = this.getCriteriaForClass(DeviceListItem.class);
Projection rowCountProjection = Projections.countDistinct("color");
criteria.setProjection(rowCountProjection);
int rowCount = ((Long) criteria.uniqueResult()).intValue();
return rowCount;
Run Code Online (Sandbox Code Playgroud)
,其目的是找出名为"color"的字段具有不同值的行数.问题是
Projections.countDistinct("color");
Run Code Online (Sandbox Code Playgroud)
返回相同数量的结果
Projections.count("color");
Run Code Online (Sandbox Code Playgroud)
即使数据库视图中有多个具有相同颜色的行.将Criteria对象转换为SQL时,我发现Hibernate生成的SQL是
select count(this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1
Run Code Online (Sandbox Code Playgroud)
当我期待它的时候
select count(distinct this_.COLOR) as y0_ from DEVICESLIST_VIEW this_ where 1=1
Run Code Online (Sandbox Code Playgroud)
为什么它没有像预期的那样工作,是否有一些补救措施?不幸的是,在这种情况下我没有选择使用HQL.