我在解决这类问题时遇到了一些困难:
我有一些嵌套实体,我正在尝试将它们保留在同一个事务中(启用 HRD)。
实体 A:
@Entity
public class A {
@Id Long id;
List<B> children;
}
实体 B:
@Entity
public class B {
@Id Long id;
}
当我尝试保留 6 个实例时(只有两个实体组,A e B)......
public void testOfy() {
ofy.getFactory().register(A.class);
ofy.getFactory().register(B.class);
List<B> list = new ArrayList<B>();
final A a0 = new A();
final B b1 = new B();
final B b2 = new B();
final B b3 = new B();
final B b4 = new B();
final B b5 = new B();
Ofy o = ofy.transaction();
try {
o.save().entities(b1).now(); list.add(b1);
o.save().entities(b2).now(); list.add(b2);
o.save().entities(b3).now(); list.add(b3);
o.save().entities(b4).now(); list.add(b4);
o.save().entities(b5).now(); list.add(b5);
a0.children = list;
o.save(a0);
o.getTxn().commit();
}
finally {
if (o.getTxn().isActive())
o.getTxn().rollback();
}
}
我得到了例外:
java.lang.IllegalArgumentException: operating on too many entity groups in a single transaction.
at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:36)
但是,如果我只放 5 个实例,一切正常......
public void testOfy() {
ofy.getFactory().register(A.class);
ofy.getFactory().register(B.class);
List<B> list = new ArrayList<B>();
final A a0 = new A();
final B b1 = new B();
final B b2 = new B();
final B b3 = new B();
final B b4 = new B();
final B b5 = new B();
Ofy o = ofy.transaction();
try {
o.save().entities(b1).now(); list.add(b1);
o.save().entities(b2).now(); list.add(b2);
o.save().entities(b3).now(); list.add(b3);
o.save().entities(b4).now(); list.add(b4);
// o.save().entities(b5).now(); list.add(b5);
a0.children = list;
o.save(a0);
o.getTxn().commit();
}
finally {
if (o.getTxn().isActive())
o.getTxn().rollback();
}
}
我正在使用 Objectify 4.0b3,有人有什么建议吗?
谢谢!