Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA, why named parameter in native query is not replaced?

I tried both, indexed and named parameters, but it doesn't work:

public interface CharacterRepository extends JpaRepository<Character, Long> {
    @Query(nativeQuery=true, value="SELECT * FROM Character WHERE pinyin like '%:keyword%'")
    List<Character> findByKeyword(@Param("keyword") String keyword);
}

The outcoming sql is:

Hibernate:

   SELECT
        * 
    FROM
        Character 
    WHERE
        pinyin like '%:keyword%'

Why is the keyword-placeholder not replaced by the parameter I actually pass?

like image 711
tobi Avatar asked Oct 17 '22 08:10

tobi


1 Answers

Your query should be like this -

@Query(nativeQuery=true, value="SELECT * FROM Character c WHERE c.pinyin like %:keyword%")
 List<Character> findByKeyword(@Param("keyword") String keyword);

Hope it help.

like image 179
Sai prateek Avatar answered Oct 21 '22 00:10

Sai prateek