1

I am trying to use like operator in native query as below:

@Query(value = "SELECT max(id) FROM EMP where id like ___:country%", nativeQuery=true)

country as input parameter for method.

getting the below error:

java.lang.IllegalArgumentException: Unknown parameter name : country

can anyone please help me on this.

Rajeswari Reddy
  • 193
  • 1
  • 6
  • 24
  • 1
    Possible duplicate of [%Like% Query in spring JpaRepository](https://stackoverflow.com/questions/25362540/like-query-in-spring-jparepository) – Mehraj Malik Apr 19 '18 at 05:54
  • Hello @Mehraj Malik, It's not duplicate to that as i have seen that and implemented the query like as same but the format is different. If i used like this %:country% then its working but not working for ___:country% format. – Rajeswari Reddy Apr 19 '18 at 06:01

1 Answers1

0

use JPA query in your repository interface like this:

@Query(nativeQuery = true, value = "SELECT max(r_id) FROM registrations WHERE r_name LIKE ?1")
int getCount (String name);

when you call this function add modulus(%) with your string like this:

@Autowired
private RegistrationRepository registrationRepository;        

    @GetMapping(value = "/reg-like")
    @ResponseBody
    public void regLike() {
        int max = registrationRepository.getCount("s%");
        System.out.println("" + max);
    }

Or you can try something like this:

@Query(nativeQuery = true, value = "SELECT max(r_id) FROM registrations WHERE r_name LIKE %?1")
int getCount (String name);

Then you can omit the modulus(%), when calling the function.

See official documentation here.

sam
  • 1,800
  • 1
  • 25
  • 47