Author
HELP !! problem with setparameter in entitymanager
jim li
Ranch Hand
Joined: May 20, 2008
Posts: 177
filterName = "first" Query q = em.createQuery( "From UserInfo where :filterName like 'jim'"); q.setParameter("filterName", filterName); q.getResultList(); my reuslt returns nothing to me. however, if i use Query q = em.createQuery( "From UserInfo where first like 'jim'"); q.setParameter("filterName", filterName); q.getResultList(); i got the result i want please help me!!!
Mark Spritzler
ranger
Sheriff
Joined: Feb 05, 2001
Posts: 17228
posted May 27, 2008 09:18:00
0
Parameters are for values, not field names. You are trying to set which field is checked, rather than a field being checked by different values. You can use a parameter to parameter "jim" the value, but not "first" the field. Mark
Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
jim li
Ranch Hand
Joined: May 20, 2008
Posts: 177
thank you for you help could you please give me couple of example? i really appreciate your help
jim li
Ranch Hand
Joined: May 20, 2008
Posts: 177
firstName = "first" first is the column in my database. if i set the parameter to 'jim' instead of "first" how could i know which column it is
jim li
Ranch Hand
Joined: May 20, 2008
Posts: 177
thank you it works now
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4967
As Mark said, the whole point of variable injection is to inject a value into the variable being used. Here's a hard coded query example: Notice the line of code: String hql="from User where loginName = 'mj' "; 'mj' is the varible name. To do variable injection, you would replace the 'mj' with a variable. It might look like this: As you can see, you were pretty close. You inject the variable name, not the column name/property name. -Cameron McKenzie
Author of Hibernate Made Easy , What is WebSphere??? , JSF 2.0 Made Easy and the SCJA Certification Guides
subject: HELP !! problem with setparameter in entitymanager