Vanitha R
Vanitha R

Reputation: 133

gae jdoquery parameters with special characters

I am writing a cloud endpoint api using JDO to fetch a list of users based on the emailid. I am passing emailId as a @Named parameter to the Api method and adding it to the query filter and i get the error message "Portion of expression could not be parsed: @gmail.com"

@Api (name="MyAppname", version="v1")
public class PersonEndpoint {

public Person validate(@Named("emailId") String emailId, @Named("role") String role){
.......

PersistenceManager pm=getPersistenceManager();
Query q = pm.newQuery(Person.class);
q.setFilter(" email == "+emailId+" && role == "+role);

try{
    person=(Person)q.execute();
}finally{
    q.closeAll();
    pm.close();
}

return person;
}

When i call the above api, the below error is thrown

javax.jdo.JDOUserException: Portion of expression could not be parsed: @gmail.com && role == collector
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:519)
at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:230)

How do we pass parameters with special characters as in email to the query filter?

Upvotes: 1

Views: 1335

Answers (1)

DataNucleus
DataNucleus

Reputation: 15577

You do not embed text strings in a text string. Instead you define a parameter, and provide the parameter value to execute().

Query q = pm.newQuery(Person.class);
q.setFilter("emailId == :theEmail && role == :theRole");
Map<String, String> paramValues = new HashMap();
paramValues.put("theEmail", myEmailParam);
paramValues.put("theRole", myRoleParam);
List<Person> persons = (List<Person>)q.executeWithMap(paramValues);

All of this is defined with examples in the JDO spec, and in the docs for DataNucleus JDO. I'd strongly suggest you read them

Upvotes: 2

Related Questions