指定列,应进行不同的计算 [英] Specfiy columns which should be considered for distinct calculation
问题描述
我正在使用 javax.persistence.criteria.CriteriaBuilder
和 javax.persistence.criteria.CriteriaQuery
进行选择一些实体.
I'm using javax.persistence.criteria.CriteriaBuilder
and javax.persistence.criteria.CriteriaQuery
to select some entities.
我现在只想选择应该由特定列指定的唯一实体.
I now want to select only the entities that are unique which should be specified by a certain column.
有一种方法 javax.persistence.criteria.CriteriaQuery#distinct
仅返回唯一的实体.
There is the method javax.persistence.criteria.CriteriaQuery#distinct
which only returns unique entities.
我宁愿需要
CriteriaQuery<T> distinct(String... columnNames)
您知道我如何在JPA CriteriaQuery
中烘烤出如此与众不同的东西吗?
Do you know how I can bake such a distinct in my JPA CriteriaQuery
?
推荐答案
以下语句没有意义:
我现在只想选择唯一的实体 由特定列指定.
I now want to select only the entities that are unique which should be specified by a certain column.
如果结果集完全相同",则将它们用"distinct"进行过滤. 如果仅某些字段相同,则实体不相同.
The result sets are filtered by 'distinct' if they are 'exactly the same'. The entities are not the same if only some fields are the same.
您可以通过以下方式在结果集上创建不同的子句:
You can make distinct clause on resultset in the following manner:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery query = builder.createQuery();
Root<Friend> c = query.from(Friend.class);
query.multiselect(c.get(Friend_.firstName),c.get(Friend_.lastName)).distinct(true);
然后,您将从Friend实体中获得firstName和lastName的唯一组合.
then you will get unique combination of firstName and lastName from Friend entities.
例如,...给我朋友的所有唯一组合,其中朋友的名字和姓氏是唯一的."但这并不意味着给我独特的朋友.
So for instance... 'Give me all unique combinations from my Friends where the firstName and lastName of the friend is unique.' But it doesn't mean give me unique friends.
这篇关于指定列,应进行不同的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!