2011-03-04 53 views
23

使用Hibernate的標準,我想執行的等效:使用Hibernate的標準和預測來選擇多個不同的列

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,有沒有解決方案呢?

感謝,

布蘭登

回答

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

'ProjectionList projList =新ProjectionList();''以來ProjectionList()'構造是不可見的無效。它應該是'ProjectionList projList = Projections.projectionList();' – destan 2012-04-03 08:27:39

+0

謝謝@destan。我確定了答案。 – 2012-04-03 12:46:49