javaspringjpaspring-dataspring-data-jpa

LIKE COALESCE in Spring JPA does not work


So my problem is the following:

I have a table I want a filter to apply to. This filter should call a query which finds based on the given information the reports.

This is how my query looks:

   @Query("SELECT r FROM Report r WHERE r.importanceLevel = COALESCE(importance,'%')" +
        "AND r.source = COALESCE(source,'%')" +
        "AND r.resolvedStatus = COALESCE(resolvedStatus,'%')" +
        "AND r.header LIKE + '%' COALESCE(query,'%') + '%'")
  List<Report> getReportsByAppliedFilter(@Param("importance") int importance, @Param("source") String source,
                                       @Param("resolvedStatus") int resolvedStatus, @Param("query") String query);

Problem is: IntelliJ does not like the following:

 LIKE + '%' COALESCE(query,'%') + '%'

Error: expected, got +

Do you have any idea how to solve this otherwise?


Solution

  • Yeah, time to go to bed. You don't use + for concat, you use concat():

    @Query(value = "SELECT r FROM Report r WHERE r.importanceLevel = COALESCE(importance,'%')" +
                "AND r.source = COALESCE(source,'%')" +
                "AND r.resolvedStatus = COALESCE(resolvedStatus,'%')" +
                "AND r.header LIKE  CONCAT('%', COALESCE(query,'%'), '%')")