I’m having some difficulty to solve this kind of problem:
I Have some nested entities and I’m trying to persist them in the same transaction (HRD enable).
Entity A:
@Entity
public class A {
@Id Long id;
List<B> children;
}
Entity B:
@Entity
public class B {
@Id Long id;
}
When I try to persist 6 instances (just two Entity Groups, 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();
}
}
I get the Exception:
java.lang.IllegalArgumentException: operating on too many entity groups in a single transaction.
at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:36)
However if I put just 5 instances everything works…
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();
}
}
I’m using Objectify 4.0b3, does anyone have any suggestion?
Thank you!
You have misunderstood what an entity group is. An entity group is not a class, it is an instance (or a group of instances).
Each of those entities represent a separate entity group. XG transactions allow a maximum of five EGs per transaction. The 6th produces the error you see.