Spring JPA存储库:如果条件不匹配,如何将默认值发送到参数

时间:2016-08-19 09:43:36

标签: spring jpa orm spring-data spring-data-jpa

@Repository
public interface WANRepository  extends JpaRepository<WANConfiguration,Long>{
    @Query("select ads from WANConfiguration as ads where ?1 BETWEEN ads.minAge AND ads.maxAge AND ads.expiryTime >?2 and ads.region=?3 and ads.gender=?4")
    List<WANConfiguration> findByAllAdsForUser(Integer userAge,Date currentTime,String region, String gender);
}

这是调用代码

List<WANConfiguration> list = fanRepository.findByAllAdsForUser(userAgeInYear, new Date(),userLocation.getRegionName(), user.getGender().name());

现在有一种情况我希望传递默认区域,即userLocation.getRegionName(),那么我该怎么做呢?在db和repository或Query中需要做哪些更改?

1 个答案:

答案 0 :(得分:0)

您可以在上面创建方法图层:

List<WANConfiguration> findByAllAdsForUser(Integer userAge,Date currentTime,String region, String gender) {
     if(region == null || region.isEmpty()) {
         String defaultRegion = "what you want";
         return findByAllAdsForUser(userAge, currentTime, defaultRegion, gender);
     } 
     return findByAllAdsForUser(userAge, currentTime, region, gender);
}