-1

我正在使用 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>
}

我调用验证 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)
4

3 回答 3

1

您尝试引用“电子邮件”的查询,但这并未声明为查询的参数,也不是Person. 因此,您会得到一个异常,即您的查询无效。也许您的查询本来是

"emailId == emailParam && role == "+role
于 2013-05-30T09:55:03.337 回答
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);
于 2014-02-12T03:15:17.433 回答
-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)
于 2013-05-30T10:54:00.877 回答