我有以下存储库代码:
@Query("SELECT " + FIELDS + " FROM Session ssn WHERE "
+ "(ssn.startDate BETWEEN :startDate AND :endDate ) AND endDate IS
:closed ? NOT : '' NULL ")
List<Session> findByDateAndStatus(@Param("startDate)Date startDate,@Param("endDate")Date endDate,@Param("closed")Boolean closed);
此代码无效。我希望此查询根据提供的布尔参数获取已关闭的会话或打开的会话。
"AND endDate IS :closed ? NOT : '' NULL "
我想根据自定义布尔参数编写HQL字符串。怎么做?
答案 0 :(得分:0)
这不起作用的原因是你混合了HQL和Java语法。相反,您需要使用HQL(或JPQL)语法来获取可以与绑定值正确比较的值。这样的事情应该有效:
... AND CASE WHEN (endDate IS NULL) THEN FALSE ELSE TRUE = :closed
另请参阅:How can I test a boolean parameter in a NamedQuery with JPA?和Is there such thing CASE expression in JPQL?