【问题标题】:JPA Query not working for latest record fetchJPA 查询不适用于最新记录获取
【发布时间】:2021-08-24 01:13:15
【问题描述】:

我正在使用这个查询来检索基于 runId 的最新记录,但是这个查询给出了任何结果。

  @Query("select vr1 from VendorResult vr1 left join VendorResult vr2 on (vr1.applicationId = vr2.applicationId "
      + "and vr1.productType=vr2.productType and vr1.runId<vr2.runId) "
      + "where vr1.applicationId=?1 and vr1.overallStatus=?2 and vr2.applicationId is null")
  List<VendorResult> getVendorResults(String applicationId,String overallStatus);

也试过了

  @Query(value = "select vs from VendorResult vs where vs.applicationId=?1 and vs.overallSatatus=?2 and vs.runId = (select max(v.runId) from VendorResult v where v.applicationId = vs.applicationId)",nativeQuery = true)
List<VendorResult> getVendorResults(String applicationId,String overallStatus);

你能帮帮我吗?

【问题讨论】:

    标签: sql oracle spring-boot jpa spring-data-jpa


    【解决方案1】:

    一页长为 1 并按 id desc 排序的分页选择将为您提供所需的内容

      @Query("select vr1 from VendorResult vr1 where vr1.applicationId=?1 and vr1.overallStatus=?2 order by vr1.runId DESC")
      List<VendorResult> getVendorResults(String applicationId,String overallStatus, Pageable pageable);
    

    调用

    repository.getVendorResults(applicationId, overallStatus, PagePageRequest.of(0, 1));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 2011-10-26
      • 2017-11-19
      • 2022-01-06
      • 1970-01-01
      • 2020-07-21
      • 2014-07-26
      • 2021-01-10
      相关资源
      最近更新 更多