Vijay Kumar Rajput
Vijay Kumar Rajput

Reputation: 1121

Not found in annotated query error with @Query

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'!

Upvotes: 14

Views: 51196

Answers (6)

SMA
SMA

Reputation: 1

Worth doing a check on the upper/lower case and typos as well which made me stuck with this for couple of hours

Upvotes: 0

Ravi
Ravi

Reputation: 338

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.

Upvotes: 2

Akinfemiwa olayode
Akinfemiwa olayode

Reputation: 105

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);

Upvotes: 0

Sandun Susantha
Sandun Susantha

Reputation: 1140

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);

Upvotes: 0

Tiina
Tiina

Reputation: 4785

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

Upvotes: 14

Vijay Kumar Rajput
Vijay Kumar Rajput

Reputation: 1121

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

Upvotes: 18

Related Questions