简体   繁体   English

如何使用谓词通过JpaRepository.findAll获得对象的不同列表?

[英]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.

相关问题 如何使用JpaRepository获取嵌套对象列表? - How can I get List of nested objects using JpaRepository? 我的jpaRepository.findAll()中的Pageable参数返回SqlSyntaxErrorException - Pageable parameter in my jpaRepository.findAll() returns an SqlSyntaxErrorException <SpringBoot / Hibernate>调用 JpaRepository.findAll 时的 InvocationException(示例) - <SpringBoot / Hibernate> InvocationException on calling JpaRepository.findAll(Example example) 为什么JpaRepository.findAll()方法从Oracle数据库中获取错误的数据? - Why is the JpaRepository.findAll() method fetching the wrong data from my Oracle database? 如何使用 Predicate 获取具有特定属性的对象列表 - How to get a List of objects having a particular property using Predicate JpaRepository findAll() 正在获取嵌套对象 - JpaRepository findAll() is fetching nested objects 在 ModelAndView 中使用 Jparepository 的 FindAll 方法 - Using Jparepository's FindAll method with ModelAndView 如何使用 JpaRepository 和嵌套的对象列表进行搜索? - How to search with JpaRepository and nested list of objects? 使用JpaRepository的findAll()导致异常-SpringBoot - findAll() using JpaRepository cause an exception - SpringBoot 如何在后端线程中使用JpaRepository? - How do I use JpaRepository in a backend thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM