Is there a way in Spring data to dynamically form the where clause?
What I want to do is have a method (which is like the findBy / get method) which runs a WHERE and
A even more simple option is to test if the parameter is null right in the JPQL query:
Exemple from my project:
@Query("select m from MessageEntity m " +
"join fetch m.demandeAnalyseEntities d " +
"where (:patientId is null or d.noPtn= :patientId) " +
" and " +
" ( :labNbr is null or d.noLab= :labNbr) " +
" and " +
" ( :reqDate is null or d.dteReq= :reqDate) " +
" and " +
" ( :reqNum is null or d.noReq= :reqNum) "
)
List findMessagesWithDemandesOnly(@Param("patientId") Long pid,
@Param("labNbr") Integer labNo,
@Param("reqDate") String reqDate,
@Param("reqNum") Integer reqNum,
Pageable pageable);