[英]How do I get a distinct list of objects with JpaRepository.findAll using a Predicate?
I have something like: 我有类似的东西:
Predicate p = ...;
List<MyObject> objectList = (List<MyObject>) myRepository.findAll(p); // myRepository is autowired
This gets the list of all MyObjects, with duplicates. 这将获取所有MyObjects的列表,其中包含重复项。 Is it possible to get a DISTINCT list instead? 是否可以获取DISTINCT列表?
Do I really need to use an EntityManager and CriteriaBuilder? 我真的需要使用EntityManager和CriteriaBuilder吗? Or can it be done with just the predicate and the repository object? 还是可以仅使用谓词和存储库对象来完成?
I'm not talking about iterating through the list manually and removing duplicates, or using any Collections API, but more on JPA or QueryDSL API. 我不是在谈论手动遍历列表,删除重复项或使用任何Collections API,而是谈论JPA或QueryDSL API。
Yes It is possible.Below are the simple steps that I can think of 是的,有可能。以下是我能想到的简单步骤
Implement hashCode() and equals() in your MyObject Class. 在MyObject类中实现hashCode()和equals()。
Create a Set based on your above list using its constructor directly which takes a Collection as an argument. 根据上面的列表直接使用其构造函数创建一个Set,该构造函数将Collection作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.