Reputation: 1121
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
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
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
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
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
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