avinash chavan
avinash chavan

Reputation: 681

Spring Data JPA NativeQuery like search

I have tried this query:

@Query(value = "SELECT distinct `id`,`customer name`,`customer number` FROM My_Data WHERE `customer number` like '%"
        + ":customerNumber" + "%'", nativeQuery = true)
public List<MyData> findDistinctByCustomerNumberContainingIgnoreCase(
        @Param("customerNumber") String customerNumber);

When I run this, I get:

java.lang.IllegalArgumentException: Parameter with that position [1] did not exist exception.

I want a native query due to some restrictions I have with my table.

Upvotes: 1

Views: 2449

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

You have put the placeholder within parentheses and the engine treats it like a plain string.

If you surround it simply with %, the engine will know to parse that adding the parentherses in the resolved query:

@Query(value = "SELECT distinct `id`,`customer name`,`customer number` FROM My_Data 
       WHERE `customer number` like %:customerNumber%", nativeQuery = true) 

Upvotes: 1

Related Questions