java - What's wrong with that JPA query, need to find a user by specific name -


i have method search name, when user passes parameter. need method tell me what's employee's role in company

the idea take data , insert combobox.

this method used find names passes parameter:

public list<user> findbyname(string name) {      entitymanager em = getentitymanager();     string sql = "select u user u upper(u.name) upper ('" + name + "%')";     list<user> u = null;     try {         u = (list<user> ) em.createquery(sql).getresultlist();      } catch (exception e) {          return null;               }     return u; } 

and here method im trying do:

public list<user>  findbyrole(){     entitymanager em = getentitymanager();             string sql = "select u user u upper(u.role) (journalist)";             list<user>  u = null;     try {         u = (list<user> ) em.createquery(sql).getresultlist();      } catch (exception e) {          return null;               }     return u; } 

code insert combobox, right?

list <user> list= new userjpacontroller().findbyrole();      for(int i=0;i<lista.size();i++){       combojournalist.additem(list.get(i).getcargo()); } 

if want use literal string journalist in query, either need put quotes, or pass parameter. otherwise jpa think journalist alias, such alias not defined in query.

so query should (see quotes around journalist):

select u user u upper(u.role) ('journalist') 

or if use parameters, better option:

string sql = "select u user u upper(u.role) (:role)"; list<user>  u = em.createquery(sql, user.class)          .setparameter("role", "journalist")          .getresultlist(); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -