JPA @Query SpEL 抛出 EL1007E,尽管进行了空检查

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

我有这样的疑问:

@Query(value =
    "select distinct t from TimesheetReport t "
    + "where t.month = :month and t.year = :year and t.approvedByMaster = false "
    + "and (:#{#rule.employee} is null or :#{#rule.employee.id} is null "
        + "or t.account.id = :#{#rule.employee.id}) "
    + "and t.id in "
    + "(select distinct dt.timesheetReport.id from DailyTime dt "
    + "where (:#{#rule.project} is null or :#{#rule.project.id} is null "
        + "or dt.project.id = :#{#rule.project.id}) "
    + "and (:#{#rule.client} is null or :#{#rule.client.id} is null "
        + "or dt.client.id = :#{#rule.client.id})) "
    + "or t.id in "
    + "(select distinct cdt.timesheetReport.id from CustomDailyTime cdt "
    + "where (:#{#rule.project} is null or :#{#rule.project.id} is null "
        + "or cdt.project.id = :#{#rule.project.id}) "
    + "and (:#{#rule.client} is null or :#{#rule.client.id} is null "
        + "or cdt.client.id = :#{#rule.client.id}) "
    + ")"
)
Set<TimesheetReport> findTimesheetsMatchingManualApprovalRule(LeaderManualApprovalRulesDTO rule, int month, int year);

我知道这是由员工对象为空引起的。

尽管首先检查规则中的员工是否为空,然后检查员工对象 id 字段是否为空,但还是会发生这种情况。我仍然收到“无法在 null 上找到属性或字段“id””。

是因为 SpEL 需要能够在触发查询之前评估每个参数吗?

spring-boot jpa spring-el
1个回答
0
投票
您可以像这样检查

@Query

参数是否为空

@Query(value = """ select t from TimesheetReport t where :#{#rule.employee == null} = true or :#{#rule.employee.id == null} = true or t.account.id = :#{#rule.employee.id} """) Set<TimesheetReport> findTimesheetsMatchingManualApprovalRule(LeaderManualApprovalRulesDTO rule);
    
© www.soinside.com 2019 - 2024. All rights reserved.