Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP !! problem with setparameter in entitymanager

 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for you help

could you please give me couple of example?

i really appreciate your help
 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you

it works now
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic