Query using “CASE WHEN” statement in WHERE causes QuerySyntaxException: unexpected AST

前端 未结 1 1993
感动是毒
感动是毒 2021-01-20 18:40

I\'m trying to make a query using Spring Data, but I cannot make it work:

@Query(SELECT t FROM Thing t WHERE name LIKE :name AND CASE WHEN (:minVal <= 0)          


        
相关标签:
1条回答
  • 2021-01-20 19:21

    It looks like Hibernate cannot evaluate the result of a CASE expression when it returns a boolean literal directly. A workaround is to make the CASE expression part of another expression, e.g. by comparing it to another boolean literal.

    So instead of:

    ... AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END
    

    Try:

    ... AND (CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END) = TRUE
    

    But looking at that expression, wouldn't it be simpler to just do:

    ... AND (:minVal <= 0 OR val <= :minVal)
    

    Is it not equivalent?

    0 讨论(0)
提交回复
热议问题