Home > Enterprise >  Set search value in native query
Set search value in native query

Time:01-12

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?

CodePudding user response:

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

e.first_name LIKE CONCAT('%', ?1, '%')
  •  Tags:  
  • Related