-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: parameterized query exception
PostPosted: Fri Nov 09, 2007 4:53 pm 
Newbie

Joined: Fri Nov 09, 2007 4:23 pm
Posts: 2
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

_________________
Bill Shirley
bshirley - frazerbilt-com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 5:42 pm 
Newbie

Joined: Wed Oct 31, 2007 5:36 pm
Posts: 13
How about something like this:

Code:
Query q;
final String hqlQuery = "from Person p where p.name.firstName like :firstname and p.name.lastName like :lastname";

q = manager.createQuery(hqlQuery);
q.setString("firstname", "%" + firstname + "%");
q.setString("lastname", "%" + lastname + "%");

result = q.list();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 6:18 pm 
Newbie

Joined: Fri Nov 09, 2007 4:23 pm
Posts: 2
modified (slightly) code:

Code:
@SuppressWarnings("unchecked")   // EJB 3.0 Persistence work-around for non-generification of the getResultsList() return value
public List<Person> findPerson(String firstname, String lastname) {
      final String hqlQuery = "from Person p where p.name.firstName like :firstname and p.name.lastName like :lastname";

      Query q = manager.createQuery(hqlQuery);
      q.setParameter("firstname", "%" + firstname + "%");
      q.setParameter("lastname", "%" + lastname + "%");
      
      return q.getResultList();
   }



it works, thanks,
i'm blaming those crufty single quotes,
i thought they were necessary

and after the @NamedQuery, even cleaner

-bill

_________________
Bill Shirley
bshirley - frazerbilt-com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.