aspose file tools
The moose likes Object Relational Mapping and the fly likes HELP !! problem with setparameter in entitymanager Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "HELP !! problem with setparameter in entitymanager" Watch "HELP !! problem with setparameter in entitymanager" New topic
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
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HELP !! problem with setparameter in entitymanager
 
Similar Threads
KODO-JDO update by query
Left Join Example
NamedQuery: Retrieve more than one field?!?
hibernate string search question
namedQuery error : node to traverse cannot be null!