【发布时间】:2013-01-16 13:20:31
【问题描述】:
我对 RequestFactory 有一点关于 Set 形状的子集合的持久性的问题。我在后端使用gwt 2.5 和requestfactory,以及Hibernate4/Spring3。我正在使用 Spring 的 open-session-in-view 过滤器,以便集合可以在我的 DAO 的保存方法中的 findByID 之后持久化。我的问题是,当子集合基于 List 时,一切似乎都可以正常工作,但是当它们基于 Set 时,并非所有来自客户端的项目都可以明显地到达服务器。
我的代码如下所示:
-根实体IndicationTemplate:
@Entity
@Table (name = "vdasIndicationTemplate")
@org.hibernate.annotations.Table ( appliesTo = "vdasIndicationTemplate", indexes =
{@Index (name = "xieIndicationTemplateCreateUser", columnNames= {"createUserID"}),
@Index (name = "xieIndicationTemplateModifyUser", columnNames= {"modifyUserID"})})
public class IndicationTemplate extends AbstractEditable <Integer> implements IEntity <Integer>, IDateable, IDescriptable {
//...
private Set <ProposalTemplate> proposalTemplates = null;
//...
@OneToMany (fetch = FetchType.LAZY, mappedBy = "indicationTemplate"
, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH})
public Set <ProposalTemplate> getProposalTemplates () {
return proposalTemplates;
}
public void setProposalTemplates (Set <ProposalTemplate> proposalTemplates) {
this.proposalTemplates = proposalTemplates;
}
//...
}
-子实体 ProposalTemplate 当然具有相反的多对一映射,并且具有 3 个子集合以及具有 3 个不同实体的相同类别。
-根实体的客户端代理:
@ProxyFor (value = IndicationTemplate.class, locator = PersistenceEntityLocator.class)
public interface IIndicationTemplateProxy extends IEntityProxy, IDeletableProxy, IDescriptableProxy {
//....
Set <IProposalTemplateProxy> getProposalTemplates ();
void setProposalTemplates (Set <IProposalTemplateProxy> proposalTemplateProxy);
}
-在客户端,我渲染根实体的属性以及子实体的列表。然后用户可以更新它们,并且更改将存储回集合中,如下所示:
Set <IProposalTemplateProxy> newList = getElementsFromUiSomehow (); //these elements can be new or just the old ones with some changes
indicationTemplate.getProposalTemplates ().clear ();
indicationTemplate.getProposalTemplates ().addAll (newList);
-然后在某个时候:
requestContext.saveIndicationTemplate ((IIndicationTemplateProxy) entityProxy)
.fire (new Receiver <IIndicationTemplateProxy> ()
-RequestContext 看起来像:
@Service (value = TemplateService.class, locator = SpringServiceLocator.class)
public interface ITemplateRequestContext extends RequestContext {
/** saves (creates or updates) one given indication template */
Request <IIndicationTemplateProxy> saveIndicationTemplate (IIndicationTemplateProxy indicationTemplate);
//....
}
问题是每个请求仅向集合服务器端添加 1 个子实体。例如,indicationTemplate 有 2 个proposalTemplate,我又添加了 4 个,然后在服务器端 saveIndicationTemplate 实体只包含 3 个而不是 6 个。如果发生,无论我之前有多少实体,我添加了多少,我只会得到 1在服务器上比以前更多。我确实在触发 requestContext 方法之前检查了代理对象,并且它已完全加载,包括它的所有子对象。最后最奇怪的是,如果我替换 Set per List(以及所有后续更改),一切正常!
使用Sets而不是Lists时RF无法将所有更改传输到服务器可能有什么问题吗?顺便说一句,在这种情况下,我确实更喜欢 Set,所以这就是我问的原因。
有人吗?
感谢您的帮助!
【问题讨论】:
-
非常感谢托马斯的回答。它看起来几乎是同一个问题,尽管 cmets 上的一些人说 EntityProxies with Set 工作......但它可能使用不同的 GWT 版本?所以,我知道现在唯一的解决方案似乎是使用 List 而不是 Set。并不是说我喜欢这个主意,但可能我就是那样做。
标签: gwt requestfactory open-session-in-view gwt requestfactory open-session-in-view