【发布时间】:2020-05-04 22:13:13
【问题描述】:
我想确保只为具有特定祖先键的实体创建一个实体。我的解决方案是将祖先查询放在事务中,检查实体是否存在,如果不存在,则创建它。
这会确保只存在一个具有特定祖先键的实体吗?
ofy().transact(new VoidWork() {
public void vrun() {
Entity entity = ofy().load().type(Entity.class)
.ancestor(ancestorKey)
.first()
.now();
if (entity == null) {
// Entity does not exist. Create it.
final Entity newEntity = new Entity(ancestorKey);
ofy().save().entity(newEntity).now();
} else {
// Entity already exists.
}
}
}
});
【问题讨论】:
标签: google-app-engine google-cloud-datastore objectify