Search code examples
javajpaspring-data-jpajpql

Spring jpa check if record exist using String date


I would like to check the DB if a record already exists for a give run date and name using Spring JPA query using two params:

  @Query("SELECT CASE WHEN COUNT(r)> 0 THEN true ELSE false END FROM REQUEST r WHERE r.NAME = :reqName AND r.RUN_DATE = :runDate")
    boolean existsRequest(@Param("reqName") String reqName,
                             @Param("runDate") String runDate);

The type of RUN_DATE in Database H2, Sql Server is a DATE field. Yet I am providing String as param for the date in format YYYY-MM-DD.

The above doesn't work for me and I was wondering how to write it correctly using both JPQL and nativeQuery ?


Solution

  • For this specific case, I think the most elegant way is to use query method:

    boolean existsByNameAndRunDate(String name, String runDate);