【发布时间】:2011-02-12 16:23:55
【问题描述】:
我正在尝试创建一个组实体。比如:
class User {
}
class UserColor {
}
...
Key key = new KeyFactory.Builder(
User.class.getSimpleName(), username).
.addChild(UserColor.class.getSimpleName(), ???).getKey();
我预先知道用于 User 对象键的唯一用户名。但我只想让应用引擎为 UserColor 实例的键值生成一个随机唯一值。
我认为这里有描述,但我不明白他们的措辞: http://code.google.com/appengine/docs/java/datastore/transactions.html
要使用系统生成的数字 ID 和实体组父级创建对象,您必须使用实体组父级键字段(例如上面的 customerKey)。将父项的键分配给父键字段,然后将对象的键字段设置为空。保存对象后,数据存储区会使用完整的键填充键字段,包括实体组父级。
这是他们的例子:
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk", value="true")
private Key customerKey;
但我不明白 - UserColor 应该是这样的吗?:
class UserColor {
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk", value="true")
private Key mKeyParent;
@Primary
private Key mKey; // leave null
}
...
Key keyParent = new KeyFactory.Builder(
User.class.getSimpleName(), username);
UserColor uc = new UserColor();
uc.setKeyParent(keyParent);
pm.makePersistent(uc); // now generated for me automatically?
是这样吗?使用这种方法,我应该可以在事务中同时使用 User 和 UserColor 对象,对吧?
谢谢
【问题讨论】: