Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter with that name [userName] did not exist

Tags:

hibernate

jpa

I have this Hibernate/JPA code:

List<User> users = getEntityManager().createQuery("from com.xxx.xxx.persistence.model.User where name    = :userName")
    .setParameter("userName", name.toLowerCase())
    .getResultList();

But I am getting this exception:

 java.lang.IllegalArgumentException: Parameter with that name [userName] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:487)
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)

If I hard code the userName in the SQL, remove the setParamter, it works. What is happening?

like image 558
mmaceachran Avatar asked Apr 22 '16 05:04

mmaceachran


1 Answers

For the people looking for other solutions, check if You did not put semicolon right after your parameter(for example if you copy native query).

Example:

Query query = getEm().createNativeQuery("Select * from a where a.id = :id;");
query.setParameter("id", 1);

This won't work, cause it will look for parameter "id" but your real parameter name is "id;".

like image 174
Kubach Avatar answered Oct 05 '22 10:10

Kubach