Search code examples
sqlspring-data-jpaspring-data

Set search value in native query


I have this Spring JPA native query:

@Query (value =
            "SELECT d.id AS id...........
                    "FROM deals_new d " +
                    "WHERE ( " +
                    "        e.first_name LIKE '%:param%' " +
                    "        OR e.last_name LIKE '%:param%' " +
                    ") " +
                    "OFFSET :offset " +
                    "LIMIT :limit ",
            nativeQuery = true)
    List<ResultDTO> getHistory(                                                   
                                                  @Param("param") String username,
                                                  @Param("offset") int offset,
                                                  @Param("limit") int limit);

What is the proper way to set search param? As you can see now it's hardcoded and set unproperly. Can you advice what is the correct way?


Solution

  • Use named placeholders like ?1, ?2 etc.,

    e.first_name LIKE CONCAT('%', ?1, '%')