java 如何在 JPA 中获得独特的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7000256/
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
How to get unique results in JPA
提问by Martin Schlagnitweit
I am using this JPA-Query (EclipseLink):
我正在使用这个 JPA-Query (EclipseLink):
SELECT e FROM Entity e GROUP BY e.label
But i get all rows in my result instead of only the unique labels.
但是我得到了结果中的所有行,而不仅仅是唯一的标签。
My second approach was using DISTINCT like i did successfully in another function which counts the distinct values, but i don't know how apply this to get the Entities:
我的第二种方法是使用 DISTINCT 就像我在另一个计算不同值的函数中成功做的那样,但我不知道如何应用它来获取实体:
SELECT COUNT(DISTINCT e.label) FROM Entity e
采纳答案by Jeremy
From the Hibernate JP-QL documentation:
select distinct e.label from Entity e
However, it seems you want the full entities, which I don't think is possible.
但是,您似乎想要完整的实体,我认为这是不可能的。
回答by dobrivoje
actually, it is possible, for example :
实际上,这是可能的,例如:
@NamedQuery(name = "WorkingDay.WorkersAtADay", query = "SELECT DISTINCT w.worker FROM WorkingDay w WHERE w.date = :Date"),