SQL 使用 Hibernate 的标准和投影来选择多个不同的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5196243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Hibernate's Criteria and Projections to Select Multiple Distinct Columns
提问by bvulaj
Using Hibernate's Criteria, I want to execute the equivalent of:
使用 Hibernate 的标准,我想执行相当于:
select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON'
I thought doing the following would yield the results I wanted:
我认为执行以下操作会产生我想要的结果:
ProjectionList projList = new ProjectionList();
projList.add(Projections.distinct(Projections.property("id.state")));
projList.add(Projections.distinct(Projections.property("id.uspsCity")));
criteria.setProjection(projList);
But, what this actually does is execute something like:
但是,这实际上是执行以下操作:
select distinct uspscity, distinct state from citycomplete where USPSCITY = 'HOUSTON'
Which throws an error, obviously.
显然,这会引发错误。
Other than not using Criteria, is there a solution for this?
除了不使用 Criteria 之外,还有其他解决方案吗?
Thanks,
谢谢,
Brandon
布兰登
回答by JB Nizet
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id.state"));
projList.add(Projections.property("id.uspsCity"));
criteria.setProjection(Projections.distinct(projList));