final SimpleFooEventHandler<SimpleBarProxy> barHandler =
new SimpleFooEventHandler<SimpleBarProxy>();
EntityProxyChange.registerForProxyType(req.getEventBus(), SimpleBarProxy.class, barHandler);
// Persist bar.
SimpleBarRequest context = req.simpleBarRequest();
final SimpleBarProxy bar = context.create(SimpleBarProxy.class);
context.persistAndReturnSelf().using(bar).fire(new Receiver<SimpleBarProxy>() {
@Override
public void onSuccess(SimpleBarProxy persistentBar) {
persistentBar = checkSerialization(persistentBar);
// Persist foo with bar as a child.
SimpleFooRequest context = req.simpleFooRequest();
SimpleFooProxy foo = context.create(SimpleFooProxy.class);
final Request<SimpleFooProxy> persistRequest =
context.persistAndReturnSelf().using(foo).with("barField");
foo = context.edit(foo);
foo.setUserName("John");
foo.setBarField(bar);
persistRequest.fire(new Receiver<SimpleFooProxy>() {
@Override
public void onSuccess(SimpleFooProxy persistentFoo) {
persistentFoo = checkSerialization(persistentFoo);
// Delete bar.
SimpleFooRequest context = req.simpleFooRequest();
final Request<Void> deleteRequest = context.deleteBar().using(persistentFoo);
deleteRequest.fire(new Receiver<Void>() {
@Override
public void onSuccess(Void response) {
assertEquals(1, fooHandler.persistEventCount);
assertEquals(2, fooHandler.updateEventCount);