使用Hibernate的标准和预测来选择多个不同的列

时间:2011-03-04 16:05:21

标签: sql hibernate criteria distinct hibernate-criteria

使用Hibernate的Criteria,我想执行相当于:

select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON'

我认为做以下操作会产生我想要的结果:

ProjectionList projList = new ProjectionList();
projList.add(Projections.distinct(Projections.property("id.state")));
projList.add(Projections.distinct(Projections.property("id.uspsCity")));
criteria.setProjection(projList);

但是,实际上这是执行类似的事情:

select distinct uspscity, distinct state from citycomplete where USPSCITY = 'HOUSTON'

显然,这会引发错误。

除了不使用Criteria之外,还有解决方案吗?

谢谢,

布兰登

1 个答案:

答案 0 :(得分:51)

ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id.state"));
projList.add(Projections.property("id.uspsCity"));
criteria.setProjection(Projections.distinct(projList));