我正在同一类型的实体之间实现友谊功能Profile
。该实体类型是根(非父)实体。
一个配置文件有一个Set<Ref<Profile>>
字段命名friends
它是吸气剂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
但我不知道通过它的方法!
提前致谢
如果环境支持,Objectify 始终会打开 XG 事务。
最有可能的是,您正在运行一个没有启用 HRD 的测试用例。您必须在 LocalDatastoreServiceTestConfig 中明确执行此操作;检查本地单元测试文档。如果您在开发实例中收到此消息,请确保选中 Eclipse 项目首选项中的“使用 HRD”复选框。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)