在查询jpa jpql中处理null

时间:2015-02-12 15:40:56

标签: jpa repository jpql

@Query("SELECT e FROM Elective e JOIN e.catalogElectives ce JOIN ce.catalog c WHERE c.catalogId = :catalogId AND e.instId = :instId")
public List<Elective> findByCatalogIdAndInstId(@Param("instId") Long instId, @Param("catalogId") Long catalogId);

我有这个代码。我希望它工作,如果有人为catalogId输入null值,那么它将忽略where子句,所以它将返回e.instId =:instId的所有位置。我该怎么做?

1 个答案:

答案 0 :(得分:0)

如何用另一个条件扩展WHERE子句?一个例子:

SELECT e 
FROM Elective e LEFT JOIN e.catalogElectives ce LEFT JOIN ce.catalog c 
WHERE (c.catalogId = :catalogId AND e.instId = :instId) OR
      (c.catalogId IS NULL      AND e.instId = :instId)