2013-05-30 73 views
1

我正在使用JDO編寫基於emailid的用戶列表的雲端點API。我傳遞EMAILID作爲@Named參數的API方法並將其添加到查詢過濾器和我得到的錯誤信息「表達的部分無法解析:@ gmail.com」帶特殊字符的gae jdoquery參數

@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; 
} 

當我打電話上面的API引發了下面的錯誤

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) 

如何在電子郵件中將特殊字符的參數傳遞給查詢過濾器?

回答

2

您不會在文本字符串中嵌入文本字符串。而是定義參數,並將參數值提供給​​。

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); 

所有這一切都與在JDO規範實施例中定義,並且在the docs for DataNucleus JDO。我強烈建議你閱讀它們

+0

感謝您的幫助!但是做出這些改變是行不通的。我得到錯誤「javax.jdo.JDOFatalUserException:解析查詢時出現意外的表達式類型。GAE(email)不支持的變量」 –

+0

如果電子郵件和角色是Person類中的字段,那麼在該語句中沒有變量。事實。如果你真的想要說你的領域人(未發佈的類)是emailId,然後把emailId。任何閱讀這樣的問題的人都不能期望成爲心理上的 – DataNucleus

+0

是'emailId'和'role'是Person類中字段的字段。 public Person validate(@Named(「emailId」)String emailId,@ Named(「role」)字符串作用,@命名(「pwd」)字符串pwd){ \t \t Person person = new Person(); \t \t \t \t PersistenceManager pm = getPersistenceManager(); \t \t查詢q = pm.newQuery(Person.class); \t \t \t \t q.setFilter(「email ==:emailIdParam && role ==:roleParam」); \t \t Map paramValues = new HashMap(); \t \t paramValues.put(「emailIdParam」,emailId); \t \t paramValues.put(「roleParam」,role); \t \t列表 persons =(列表)q.executeWithMap(paramValues);' –