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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:20:59  来源:igfitidea点击:

Getting unique result in Hibernate

javahibernatecriteria

提问by Shashi

how can we get distinct result by using criteria in hibernate.

我们如何通过在休眠中使用标准来获得不同的结果。

回答by trunkc

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

See also https://forum.hibernate.org/viewtopic.php?t=941669

另见https://forum.hibernate.org/viewtopic.php?t=941669

回答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()