13

I am using spring data with REST. I have a table country and an Entity corresponding to it called Country.java

I have annotated my method in CountryRepositopry as

public interface CountryRepository extends Repository<Country, Short> {

    @RestResource(path = "bycode3")
        @Query("select c from Country c where c.codeAlpha3=?1 and c.active=1")
        Country findCountryByCodeAlpha3(@Param("code") String countryCode);
 }

I am getting following exception while starting tomcat-

Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract com.persistence.entity.common.Country com.persistence.repository.CountryRepository.findCountryByCodeAlpha3(java.lang.String) but parameter 'code' not found in annotated query 'select c from Country c where c.codeAlpha3=?1 and c.active=1'!
Vijay Kumar Rajput
  • 1,071
  • 1
  • 10
  • 30

5 Answers5

18

I got the fixed

Query needs be modified as

@Query("select c from Country c where c.codeAlpha3=:code and c.active=1") 

Instead of ?1 it should be :code

Vijay Kumar Rajput
  • 1,071
  • 1
  • 10
  • 30
12

The text xxx in @Param("xxx") must be used in Query value.

Tiina
  • 4,285
  • 7
  • 44
  • 73
2

just an add-on to existing answers

@Query("select c from Country c where c.codeAlpha3=: code and c.active=1") 

don't put space between ":code" as ": code" or else you will get error.

Ravi
  • 338
  • 1
  • 12
0

If you are using @Param annotation, you should use same as ur entity class parameter and also use it same in "(@Param("code") String countryCode)" also:

public interface CountryRepository extends Repository<Country, Short> {

    @RestResource(path = "bycode3")
        @Query("select c from Country c where c.codeAlpha3=?1 and c.active=1")
        Country findCountryByCodeAlpha3(@Param("code") String countryCode);
 }

According to this, it should be changed like this:

@RestResource(path = "bycode3")
    @Query("select c from Country c where c.codeAlpha3=?1 and c.active=1")
    Country findCountryByCodeAlpha3(@Param("codeAlpha3") String countryCode);
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Sandun Susantha
  • 1,010
  • 1
  • 10
  • 11
0

You can try this, but always make sure that the string value in @Param("code") is the same as the named variable value you want to use in your query.

@RestResource(path = "bycode3")
    @Query("select c from Country where c.codeAlpha3=:code and c.active=1")
    Country findCountryByCodeAlpha3(@Param("code") String countryCode);