gpt4 book ai didi

google-app-engine - GAE JDO persistencemanager GetObjectById 返回没有这样的对象?

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:54 24 4
gpt4 key购买 nike

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

我有这个用户数据管理器类,它使用单例 PMF 管理我所有的 CRUD 事务。

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

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 ?由于所有这些问题,我正在考虑回到 Key。我认为 encodedKey 会更便携,因为我正在使用 GAE 服务器向我的 Android 客户端提供数据。

最佳答案

当您将该字段标记为生成其值时,调用 setEncodedKey() 有何意义? (使用 valueStrategy=IDENTITY)。如果生成了某些东西(当插入到数据库中时,就像那样),那么您传递给 setEncodedKey 的值将被忽略,通过调用 pm.getObjectId(obj).

关于google-app-engine - GAE JDO persistencemanager GetObjectById 返回没有这样的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com