In Spring Data I have 2 very large queries which are essentially identical, but with small differences. I don't want to duplicate the methods for both queries. Suppose I have
Method 1
@Query(value = ".. " +
" .. " +
//... big query
"...")
public List<Bean> getResult(@Param("studyId") long studyId);
Method 2
@Query(value = ".. " +
" .. " +
//... big query, after WHERE:
" and (:startDate is null or :startDate = '' or r.recall_date >= to_date(cast(:startDate as TEXT) " +
"...")
public List<Bean> getResult(@Param("studyId") long studyId, @Param("startDate" String startDate);
My goal is:
1) Parameterize the @Query string so that it can either take or omit the optional additional WHERE as a sub-string.
2) Somehow refactor the methods so they don't call separate SQL. The only difference is the additional parameter in Method 2.
Is this possible?
@Query. It only allows constants.