【问题标题】:I am not getting Expected results using spring Data JPA我没有使用 Spring Data JPA 获得预期结果
【发布时间】:2020-06-15 08:41:11
【问题描述】:

我有一个存储库:MEOfferRepository

@Repository
public interface MEOfferRepository extends JpaRepository<MEOfferDetailEntity, Long> {

  @Query("select carrierId, carrierName from MEOfferDetailEntity entity where "
      + "(:countryCode is null or entity.carrierGeoAreaCode =:countryCode)")
  CompletableFuture<Set<MEOfferDetail>> findCarrierByCountryCode(
      final @Param("countryCode") String countryCode);

}

MEOfferDetailEntity 是实体,MEOfferDetail 是该实体的投影。

如果我提供了一个有效的 countryCode 并且我从这个查询中得到了预期的结果,那么这工作正常,问题是当我将国家代码传递为 null 时。

我期待 :countryCode is null 运行,因为 null 是 null 并且查询将在没有 where 子句的情况下获取结果。

这个查询在 MySql 工作台中给出了预期的结果,但在 Spring Boot JPA 中我得到一个空集作为响应,这不应该是这种情况。

【问题讨论】:

    标签: mysql spring spring-data-jpa projection


    【解决方案1】:

    我曾经遇到过同样的问题,然后我更改了语法并且它起作用了。

    @Repository
    public interface MEOfferRepository extends JpaRepository<MEOfferDetailEntity, Long> {
    
      @Query("select carrierId, carrierName from MEOfferDetailEntity entity where " +
          "entity.carrierGeoAreaCode = coalesce(:countryCode, entity.carrierGeoAreaCode)")
      CompletableFuture<Set<MEOfferDetail>> findCarrierByCountryCode(
          final @Param("countryCode") String countryCode);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 2020-06-11
      • 2023-02-07
      • 2022-01-24
      相关资源
      最近更新 更多