File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
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: 180
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: 17249
    
    6

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: 180
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: 180
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: 180
thank you

it works now
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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
 
Don't get me started about those stupid light bulbs.
 
subject: HELP !! problem with setparameter in entitymanager
 
Similar Threads
namedQuery error : node to traverse cannot be null!
Left Join Example
hibernate string search question
NamedQuery: Retrieve more than one field?!?
KODO-JDO update by query