Search code examples
gwtrequestfactory

Is is possible to attach EntityProxy to another RequestContext ? And should I?


I have a little form that edits my entity. I create that entity using one instance of RequestContext and then in handler of "save" button I'm trying to persist it using another instance of RequestContext.

Attempt to persist fails with expected exception: java.lang.IllegalArgumentException: Attempting to edit an EntityProxy previously edited by another RequestContext

The questions are:

  • Is is possible to attach EntityProxy to another RequestContext ?
  • Is it right thing to do ? Or I should reuse one RequestContext saving it as property of my class ?

Solution

  • Reuse the existing one. If you need a new one, merge them. But it really doesn't make sense to make a new one. Here's why:

    When you make a RF call to the server, it goes through several steps, as it is geared mostly toward how to read and persist entities, and the various value properties they work with.

    • Create or retrieve any of the instances to be dealt with
    • Apply setters to these instances, new or existing, and validate them
    • Run the service invocations, either as methods invoked on the entities, static calls, or service calls.

    These three steps are done in this order to ensure that the object modified and then passed to a service call makes sense when it gets there. Future calls (i.e. other requests) then probably do not need to make those same changes to the same entities, and if they do, then they need to make the changes themselves.

    A given RequestContext consists of all of these things then. If you had two requests, and one represented the setters to be called (the edits from the form) and the other the service request, firing one means only calling the setters, but not the service call to save it, while firing the other means only call save without the service call.

    After an EntityProxy has been marked as being edited by one request context, attempting to use it in another one is almost certainly an error, so the exception you are seeing is thrown. Use the existing one, or use RequestContext.append if needed to switch to a new RequestContext type to actually run the save operation.

    RequestFactory isn't RPC - your objects aren't just Java Beans, but are proxies (either EntityProxy or ValueProxy) of some server object, and requests are used to manipulate them asynchronously.