JPA @Query SpEL抛出EL 1007 E,尽管检查为空

r3i60tvu  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(162)

我有这样的疑问:

@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);

字符串
我知道这是由employee对象为null引起的。
尽管首先检查规则中的雇员是否为空,然后检查雇员对象ID字段是否为空,但仍会发生这种情况。我仍然得到“Property or field 'id' cannot be found on null"。
这是因为SpEL需要能够在触发查询之前评估每个参数吗?

kknvjkwl

kknvjkwl1#

您可以像这样检查jpa @Query注解参数是否为null

@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
);

字符串

相关问题