当我将一个对象持久化到数据存储区时,何时(以及如何)可以获得我刚刚持久化的特定对象的 key ?例如,如果我有:
@PersistenceCapable
public class Employee {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
...
}
查询类:
public class EmployeeQuery {
// Persist a single Employee
public void persistEmployee(Employee e) {
// 1. Can I get the id at this point?
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(e);
// 2. Can I get the id at this point?
}
finally {
pm.close();
// 3. Can I get the id at this point?
}
}
...
}
可以在此处找到 PersistenceManager 和 PMF 信息:http://code.google.com/appengine/docs/java/datastore/jdo/overview.html#Getting_a_PersistenceManager_Instance
如上所述,在上述区域(1、2 或 3)中,我可以在哪里获得该特定对象的自动生成 ID?另外,如何获取该特定对象的 id?关于如何有效地做到这一点有什么建议吗?
谢谢。
最佳答案
一旦对象被持久化,您就可以在第2点中获取 key 。 第1点太早,发生异常时也会调用第3点,因此不能保证有生成的 key 。
作为official docs say : “保存实例时会填充实例的长键字段。”
关于java - 在JDO(AppEngine)中,持久化一个对象后,我如何获取该特定对象的 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5135018/