选择具有不同于使用spring jpa出现的字段的实体

问题描述 投票:0回答:1

我有一个名为证书的实体

@Entity
public class CertificateData {
    @Id private String id;
    private long expireDate;
    private long createDate;
    private int status;
    private String subjectDN;
...
}

许多证书可以具有相同的主题,并且我想选择所有具有不等于3的字段subjectId的计数的证书。

public List<CertificateData> findAllByRedundancy() {
    Map<String, Integer> subjectDNCount = new HashMap<>();
    Map<String, List<CertificateData>> subjectDNCertificates = new HashMap<>();

    List<CertificateDto> certificateDataList =
       this.certificaterepository
       .findAll().forEach(certificateData -> {
          String subject = certificateData.getSubjectDN();
          if(subjectDNCount.containsKey(subject)){
              subjectDNCount.put(subject, subjectDNCount.get(subject)+1);
              subjectDNCertificates.get(subject).add(certificateData);
          }
          else{
              subjectDNCount.put(subject, 1);
              subjectDNCertificates.put(subject, new ArrayList<>());
              subjectDNCertificates.get(subject).add(certificateData);
         }
  });
  List<CertificateDto> result = new ArrayList<>();
  subjectDNCount.forEach((s, i) -> {
     if(i!=3){
        result.addAll(subjectDNCertificates.get(s));
  }
  });

  return result;
}

我试图使用带有查询注释的Spring JPA来做同样的事情,它看起来像这样:

@Query("select c from CertificateData c group by c.subjectDN Having count(c) <> 3")
List<CertificateData> findAllByRedundancy();

但是它不会返回所有证书。它仅返回不重复的subjectDN的证书。

我有一个名为certificate的实体@Entity公共类CertificateData {@Id私有字符串ID;私人长久到期日期;私人长createDate;私人int身份;私人...

java spring-boot spring-data-jpa jpql
1个回答
0
投票

注释中的查询正在选择组,而不是“证书数据”记录。获取证书数据记录本身的一种方法是使用子查询查找所需的subjectDN值(您已经拥有),然后让外部查询查找包含这些subjectDN值的记录。我还没有测试过,但是在JPQL中会是这样的:

© www.soinside.com 2019 - 2024. All rights reserved.