【发布时间】:2013-01-21 17:47:53
【问题描述】:
我正在实现同一类型的实体之间的友谊功能Profile。此实体类型是根(非父)实体。
Profile 有一个名为 friends 的 Set<Ref<Profile>> 字段,它是 getter getFriends()。
代码如下:
public boolean makeFriends(final Profile profile1, final Profile profile2) {
final Ref<Profile> profileRef1 = Ref.create(profile1);
final Ref<Profile> profileRef2 = Ref.create(profile2);
boolean result = false;
// test to avoid useless transaction
if (!profile1.getFriends().contains(profileRef2) && !profile2.getFriends().contains(profileRef1)) {
// add to friends (Set<Ref<Profile>>) the Ref of each other
result = ofy().transact(new Work<Boolean>() {
@Override
public Boolean run() {
profile1.getFriends().add(profileRef2);
profile2.getFriends().add(profileRef1);
ofy().save().entities(profile1, profile2).now();
return true;
}
});
}
return result;
}
这段代码给我一个:
java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG
即使 Objectify 文档说:
Objectify 不需要特殊标志来启用跨组 交易。如果您访问一个实体组中的多个实体组 交易,交易为XG交易。如果你这样做 访问只有一个,它不是。 5 EG 的标准限制适用于所有 交易。
那么,为什么我的交易失败了?
我的代码应该涉及两个实体组(每个 Profile 一个),所以在 5 个限制之后。
查看TransactionOptions.Builder.withXG 文档,我应该先致电TransactionOptions.Builder.withXG(true);。
这个方法返回一个TransactionOptions,但我不知道传递它的方法!
提前致谢
【问题讨论】:
-
阅读此 Oracle 社区文章:[stackoverflow.com/questions/30312192/… [1]:stackoverflow.com/questions/30312192/…
标签: google-app-engine transactions objectify