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?
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;".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With