gae cloud endpoint api jdo查询过滤器电子邮件不受支持

时间:2013-05-29 18:57:30

标签: google-app-engine email filter jdo notsupportedexception

我正在使用JDO编写云端点api,以根据emailid获取用户列表。我将电子邮件ID作为@Named参数传递给电子邮件并将其添加到查询过滤器,我收到错误消息“解析查询时出现意外的表达式类型.GAE不支持变量(电子邮件)”

仅供参考,gae版本为1.8

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

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

    PersistenceManager pm=getPersistenceManager();
    Query q = pm.newQuery(Person.class);

    q.setFilter(" email == emailParam && role == "+role);
    q.declareParameters("String emailParam");

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

    return person;
}

}

有什么建议吗?

这是Person类

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Person {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
    @Persistent
private String emailId;
    @Persistent
private String role;
    <getters and setters here>
}

我在调用validate API时看到的异常

javax.jdo.JDOFatalUserException: Unexpected expression type while parsing query. Variables not supported by GAE (email)
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:498)
at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:252)

3 个答案:

答案 0 :(得分:1)

您尝试查询“电子邮件”但尚未将其声明为查询的参数,也不是Person的字段。因此,您会收到查询无效的异常。也许你的查询是

"emailId == emailParam && role == "+role

答案 1 :(得分:0)

问题是您没有正确识别参数。

将您的代码更改为:

Query q = pm.newQuery(Person.class);
q.declareParameters("String emailParam, String roleParam");
q.setFilter(" emailId == emailParam && role == roleParam");
q.setUnique(true); // This is needed if only returning one object otherwise it returns a list

try{
    person=(Person)q.execute(emailId, role);

答案 2 :(得分:-2)

我将电子邮件更改为emailId并且现在没有使用emailParam,因为我使用的是emailId,因为它已传递到api方法。所以代码现在看起来像

public Person validate(@Named("emailId") String emailId, @Named("role") String role){
     Query q = pm.newQuery(Person.class);
    q.setFilter(" email == "+emailId+" && role == "+role);
            person=(Person)q.execute();

}

现在我看到了一个不同的错误,因为电子邮件ID中有@。我们如何将这些参数传递给查询?

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)