通过RequestFactory更新多个实体
我有一个包含产品和专家的 GWT 应用程序,一个产品可以有多个专家(一对多关系)。我在使用两个相关实体混合和组合请求时遇到一些麻烦:
ProductProxy 如下所示:
@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();
}
我有一个可以编辑产品的对话框,因此它执行以下操作:
ProductProxy selectedProduct; // This comes from a function that delivered all products
productRequest = MyAplication.getRequestFactory().productRequest();
this.product = productRequest.edit(selectedProduct);
编辑产品时,您还可以向该产品添加专家:
@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);
[...]
}
我有一个单独的对话框出现。在此对话框中,我尝试使用在作为参数传递之前编辑过的产品创建一个 Expert:
expertRequest = MyApplication.getRequestFactory().expertRequest();
ExpertProxy expert = expertRequest.create(ExpertProxy.class);
expert.setProduct(product); // product comes from the productRequest code
如果我尝试这样做,则在执行 setProduct 时会收到错误,因为来自 ProductRequest 的请求与来自 ExpertRequest 的请求混合。
解决这个问题的最佳方法是什么?我可以通过原始产品请求添加专家吗?我应该从产品中获取 ID 并仅在我的专家请求中使用它吗?或者我应该添加特定的服务器功能来为产品添加专家?或者有更好的选择吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您应该对所有编辑/创建使用相同的
RequestContext
实例。每个
RequestContext
都会累积要在服务器上重播的操作(新代理和在代理上调用的 setter)和调用(服务方法调用)。当您fire()
时,这些都会作为批次发送。因此,您的ExpoertProxy
必须从与您添加到的ProductProxy
相同的RequestContext
创建。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 youfire()
. So yourExpoertProxy
must be created from the sameRequestContext
as theProductProxy
you add it to.