Search code examples
javagwtrequestfactory

Update multiple entities via RequestFactory


I have a GWT applications that contains Products and Experts, and one Product can have multiple Experts (OneToMany relationship). I have some trouble mixing and combining the requests using two entities that are related:

The ProductProxy looks like this:

@ProxyFor(myapplication.server.domain.Product.class)
public interface ProductProxy extends EntityProxy {

    Long getId();

    public String getName();
    public void setName(String name);

    [...]

    EntityProxyId<ProductProxy> stableId();

    Set<ExpertProxy> getExperts();
}

I have a Dialog that can edit the Product, so it does a:

ProductProxy selectedProduct; // This comes from a function that delivered all products
productRequest = MyAplication.getRequestFactory().productRequest();
this.product = productRequest.edit(selectedProduct);

When editing the product, you can also add experts to this product:

@ProxyFor(myapplication.server.domain.Expert.class)

public interface ExpertProxy extends EntityProxy {
    public Long getId();
    public void setId(Long id);

    public ProductProxy getProduct();
    public void setProduct(ProductProxy product);
    [...]
}

I have a separate DialogBox that appears for it. Inside this dialog box, I try to create an expert with the product that is edited before passed on as parameter:

expertRequest = MyApplication.getRequestFactory().expertRequest();
ExpertProxy expert = expertRequest.create(ExpertProxy.class);
expert.setProduct(product); // product comes from the productRequest code

If I try this out, I get an error when doing the setProduct, because the request from productRequest is mixed with a request from expertRequest.

What's the best way to fix this? Can I add an expert via the original productRequest? Should I get the Id from the product and only use this in my Expert Request? Or should I add specific Server functions to add an expert to a product? Or are there better options?


Solution

  • Yes, you should use the same RequestContext instance for all your edits/creates.

    Each RequestContext accumulates operations (new proxies and setters called on proxies) and invocations (service method calls) to be replayed on the server. This is all sent as a batch when you fire(). So your ExpoertProxy must be created from the same RequestContext as the ProductProxy you add it to.