[英]Ignoring condition if the parameter is null in Spring Data JPA
我目前正在开发我的项目的搜索和过滤功能。 我正在尝试使用此请求正文检索数据:
{
"researchers": [
"Jhon Doe"
],
"unit": null,
"agency":null,
"date": null,
"status": null,
"budget": null
}
以下是 ReasearchRepository 中检索此数据的方法:
@Query("select r from Research r " +
"left join r.fundingAgencies fundingAgencies " +
"left join r.researchers researchers " +
"where (:budgetStart is null or :budgetEnd is null or r.budget between :budgetStart and :budgetEnd )" +
"and (:startDate is null or r.startDate >= :startDate) " +
"and (:endDate is null or r.endDate <= :endDate) " +
"and (:agencyNames is null or fundingAgencies.agencyName in :agencyNames) " +
"and (:unitNames is null or r.deliveryUnit.unitName in :unitNames )" +
"and (:names is null or researchers.name in :names ) " +
"and (:researchStatuses is null or r.researchStatus in :researchStatuses)")
List<Research> findAdvanced( @Param("budgetStart") Double budgetStart,
@Param("budgetEnd") Double budgetEnd,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate,
@Param("agencyNames") Collection<String> agencyNames,
@Param("unitNames") Collection<String> unitNames,
@Param("names") Collection<String> names,
@Param("researchStatuses") Collection<ResearchStatus> researchStatuses);
我的问题是当我试图从请求中的给定值中查找数据时,它返回一个空列表[],即使它存在于数据库中。 我想要实现的是,如果参数为空,它会忽略查询中的特定条件,仍然会返回数据。
我刚刚关注了Baeldung的文章。
我还找到了另一篇关于使用 Criteria API 的文章,但我需要很多时间才能理解。 我的项目有点匆忙。
TIA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.