java 在 Hibernate 中获得独特的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915286/
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
Getting unique result in Hibernate
提问by Shashi
how can we get distinct result by using criteria in hibernate.
我们如何通过在休眠中使用标准来获得不同的结果。
回答by trunkc
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
回答by waxwing
A more flexible solution may be:
更灵活的解决方案可能是:
criteria.setProjection(Projections.distinct(Projections.property("property")));
回答by Salandur
depends on your query/criteria. if you provide a unique id you can call criteria.uniqueResult() otherwise you call criteria.setMaxResults(1) and call criteria.uniqueResult()
取决于您的查询/标准。如果你提供一个唯一的 id 你可以调用criteria.uniqueResult() 否则你调用criteria.setMaxResults(1) 和调用criteria.uniqueResult()

