I am having a problem parameterizing a query.
It seems that the Query class isn't able to parse my query string to find the parameter (or i'm doing it wrong).
Both indexed (?1) and named (:name) parameters are failing.
(I'm trying to clean up my code, and convert some queries to annotated NamedQueries, but I need to get the parameterization working first.)
tia,
Hibernate version: 3.2.4 sp1
Code causing the exception:
Code:
public List<Person> findPerson(String firstname, String lastname) {
List<Person> result;
Query q;
q = manager.createQuery("FROM Person p WHERE p.name.firstName LIKE '"+ firstname +"' and p.name.lastName LIKE ':varname'");
q.setParameter("varname", lastname);
// tried ?1 as well - same problem,
// treating names as substrings - works fine
// q = manager.createQuery("FROM Person p WHERE p.name.firstName LIKE '"
// + firstname + "%' and p.name.lastName LIKE '" + lastname + "%'");
result = q.getResultList();
return result;
}
Truncated stack trace of QueryParameterException:Code:
Caused by: org.hibernate.QueryParameterException: could not locate named parameter [varname]
at org.hibernate.engine.query.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:75)
at org.hibernate.engine.query.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:81)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:413)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:383)
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:171)
at com.frazerbilt.general.PersonAccessBean.findPerson(PersonAccessBean.java:49)
Name and version of the database you are using:
mysql-5.0.45-osx10.4-i686
Also:
Eclipse
JBoss 4.2.1.GA