我的应用程序中有两个实体和一个辅助bean。以下是它的简化版本:
控制器和型号:
class BackingBean {
private List<A> collectionOfA;
private A currentA;
private B currentB;
private String newDescription;
// accessors
public void prepareForUpdate(ActionEvent e) {
currentA = (A) e.getComponent().getAttributes().get("a");
currentB = (B) e.getComponent().getAttributes().get("b");
}
public void save(ActionEvent e) {
// method to save b
b.setName("changing the name");
b.setSomeNumber(2);
b.setDescription(newDescription);
entityManager.merge(b);
}
}
@Entity
class A {
private String name;
@OneToMany
private List<B> bs;
}
@Entity
class B {
private String name;
private String description;
private int someNumber;
}
查看:
<div>
<!-- some popup with inputs for updating B -->
<h:inputText value="#{backingBean.currentB}" />
<h:commandLink actionListener="#{backingBean.save}" />
</div>
<ui:repeat value="#{backingBean.collectionOfA}" var="a">
<h:outputText>#{a.name}</h:outputText>
<ui:repeat value="#{a.bs}" var="b">
#{b.name}
#{b.description}
#{b.someNumber}
<h:commandLink actionListener="#{backingBean.prepareForUpdate}">
<f:attribute name="a" value="#{a}" />
<f:attribute name="b" value="#{b}" />
</h:commandLink>
</ui:repeat>
</ui:repeat>
假设当我点击commandLink
prepareForUpdate()
时弹出窗口显示,我的问题是:当我保存currentB
实体时,实体的每个字段都会更新风景。但是,在一瞬间之后,字段b.description
将再次使用旧值进行渲染。当我检查数据库时,实际上更新了描述,就像我刷新页面一样。
有关为何发生这种情况的任何想法?