GAE JDO persistencemanager GetObjectById返回No such object?

时间:2012-10-11 15:45:01

标签: google-app-engine jdo

我一直试图让我的GAE代码运行。我在使用encodedKey检索用户时遇到问题:

我有这个User Data Manager类,它使用singleton PMF管理我的所有CRUD事务。

我有用户,使用编码的字符串作为我的密钥:

public class Users implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String encodedKey;
@Persistent
@Extension(vendorName = "datanucleus", key = "gae.pk-id", value = "true")
private Long keyId;
 --- code continues ----

我在DM类中有这个createUser方法

public static void createUser(Users user) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        Key key = KeyFactory.createKey(Users.class.getSimpleName(),
                user.getEmail());
        user.setEncodedKey(KeyFactory.keyToString(key));
        pm.makePersistent(user);
    } catch (Exception e) {
        logger.log(Level.SEVERE, e.toString());
    } finally {
        pm.close();
    }
}

问题在于要检索这段代码:

    public static Users retrieveUser(String encodedKey) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Users user = null;
    try {
//          Key key = KeyFactory.createKey(Users.class.getSimpleName(),
//                  user.getEmail());
// I tried recreating the key with user's email, but it does not work either            
        pm.getObjectById(KeyFactory.stringToKey(encodedKey));
    } catch (Exception e) {
        logger.severe(e.toString());
    } finally {
        pm.close();
    }
    return user;
}

它给了我以下错误:

 SEVERE: javax.jdo.JDOObjectNotFoundException: No such object
 FailedObject:Users("t")
 NestedThrowables:org.datanucleus.exceptions.NucleusObjectNotFoundException: No such object
无论如何,

“t”是虚拟用户的电子邮件。

如何使用encodedKey来获取使用PMF的实体? 目前,我正在使用电子邮件和查询进行识别,但按键获取对象应该有效。

编辑

我对模型对象进行了以下更改:

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String encodedKey;
@Persistent
@Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
private String keyName;

基于此和谷歌的文档:

应用可以在使用带有名称的密钥保存之前填充此值,也可以将其保留为空。如果编码的键字段为空,则在保存对象时使用系统生成的密钥填充该字段。

我认为只需坚持下去就可以了:

public static void createUser(Users user) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        user.setKeyName(user.getEmail()); 
        pm.makePersistent(user);
    } catch (Exception e) {
        logger.log(Level.SEVERE, e.toString());
    } finally {
        pm.close();
    }
}

但是,没有这样的运气,无论我是否先设置密钥名,它都会引发我的错误:

SEVERE: javax.jdo.JDOFatalUserException: Invalid primary key for entity.Users.  Cannot have a null primary key field if the field is unencoded and of type String.  Please provide a value or, if you want the datastore to generate an id on your behalf, change the type of the field to Long.
NestedThrowables:
org.datanucleus.exceptions.NucleusFatalUserException: Invalid primary key for entity.Users.  Cannot have a null primary key field if the field is unencoded and of type String.  Please provide a value or, if you want the datastore to generate an id on your behalf, change the type of the field to Long.

我没理解,为什么不为我的新实体生成新密钥?由于所有这些问题,我正在考虑回到Key。我认为encodedKey会更便携,因为我正在使用GAE服务器向我的Android客户端提供数据。

2 个答案:

答案 0 :(得分:1)

当您标记该字段以生成其值时,调用 setEncodedKey ()有什么意义? (使用 valueStrategy = IDENTITY )。如果生成某些内容(当插入数据库时​​,就像那样),则会忽略传入 setEncodedKey 的值,这可以通过调用 pm.getObjectId(obj)

答案 1 :(得分:0)

您需要使用@PersistenceCapable注释为您的班级添加注释。有关更多信息,请查看Defining Data Classes with JDO