Removing an object from a collection and page refresh
fausto72 Nov 24, 2008 3:45 PMHi all,
software configuration:MySQL Server 5.0, JBossAS 4.2.2.GA, Seam 2.0.0.GA and RichFaces 3.2.0
I have this situation: relationship oneToMany between two objects, Dossier and Allegato; in a page I show the Dossier entity and a list of associated Allegato entities (listaAllegati collection). Through a modal panel it is possible to add and remove Allegato entities.
The problem is that when an Allegato is removed from the collection, the corresponding list is not refreshed (but the Allegato is correctly removed from the database). On the other side, closing the web page and reopening it on the same Dossier shows updated list of Allegato. The add operation works correctly, i.e., the page is refreshed and the new Allegato is shown in the list.
The page where the information is shown
... <f:subview> <rich:modalPanel id="allegaPopup" minWidth="300" minHeight="200" zindex="2000"> <f:facet name="header"> <h:outputText value="Allega documento" /> </f:facet> <f:facet name="controls"> <h:graphicImage value="/img/close.png" style="cursor:pointer" id="hidelink"/> <rich:componentControl for="allegaPopup" attachTo="hidelink" operation="hide" event="onclick"/> </f:facet> <h:form id="allega" enctype="multipart/form-data"> <h:panelGrid> <s:fileUpload data="#{allegatoManager.file}" fileName="#{allegatoManager.fileName}" contentType="#{allegatoManager.contentType}" styleClass="sfoglia"/> <h:panelGroup styleClass="gruppoBottoni"> <h:commandButton value="Salva" action="#{allegatoManager.add}"/> <h:commandButton value="Annulla" onclick="javascript:Richfaces.hideModalPanel('allegaPopup')"/> </h:panelGroup> </h:panelGrid> </h:form> </rich:modalPanel> </f:subview> ... <rich:dataTable id="allegatoList" var="_allegato" value="#{dossierHome.instance.listaAllegati}" rendered="#{not empty dossierHome.instance.listaAllegati}"> <rich:column> <f:facet name="header"> Nome del file </f:facet> <s:link action="#{allegatoManager.remove(_allegato.id)}" onclick="if (!confirm('Vuoi veramente eliminare questo allegato?')) return false;"> <f:param name="allegatoId" value="#{_allegato.id}" /> <h:graphicImage styleClass="image" value="/img/cancella2.png"/> </s:link> <s:link action="#{allegatoManager.download}" target="_blank"> <f:param name="allegatoId" value="#{_allegato.id}" /> <h:graphicImage styleClass="image" value="/img/visualizza.png"/> </s:link> <s:link action="#{allegatoManager.download}" target="_blank"> <f:param name="allegatoId" value="#{_allegato.id}" /> <h:outputText value="#{_allegato.fileName}"/> </s:link> </rich:column> <rich:column> <f:facet name="header"> Lunghezza [KByte] </f:facet> #{_allegato.length} </rich:column> </rich:dataTable>
The Dossier entity:
@Entity @Indexed @Name("dossier") public class Dossier implements Serializable { private static final long serialVersionUID = 1L; private Collection<Allegato> listaAllegati; public Dossier() { listaAllegati = new ArrayList<Allegato>(); } @OneToMany(targetEntity=Allegato.class, mappedBy="dossier", cascade=CascadeType.REMOVE) public Collection<Allegato> getListaAllegati() { return listaAllegati; } public void setListaAllegati(Collection<Allegato> listaAllegati) { this.listaAllegati = listaAllegati; } }
The Allegato entity:
@Entity public class Allegato implements Serializable { private static final long serialVersionUID = 1L; //TODO private Long id; byte[] file; String fileName; String contentType; // mime type int length; // lunghezza in bytes private Dossier dossier; @Id @GeneratedValue public Long getId() { return id; } @ManyToOne(optional=true) public Dossier getDossier() { return dossier; } public void setDossier(Dossier dossier) { this.dossier = dossier; } }
The action class AllegatoManagerBean:
@Name("allegatoManager") @Stateless @AutoCreate @Scope(ScopeType.SESSION) public class AllegatoManagerBean implements AllegatoManager { @RequestParameter Long allegatoId; @In @Out DossierHome dossierHome; @In(create=true) @Out AllegatoHome allegatoHome; // allegato byte[] file; String fileName; String contentType; // mime type public String add() { Allegato allegato = new Allegato(); int i = fileName.lastIndexOf('\\'); allegato.setFileName((i > -1) ? fileName.substring(i+1) : fileName); allegato.setContentType(contentType); allegato.setFile(file); allegato.setLength(file.length); allegatoHome.setInstance(allegato); allegatoHome.getInstance().setDossier(dossierHome.getInstance()); allegatoHome.persist(); dossierHome.getInstance().getListaAllegati().add(allegatoHome.getInstance()); dossierHome.update(); return null; } public String remove() { if(null != allegatoId) { allegatoHome.setId(allegatoId); dossierHome.getInstance().getListaAllegati().remove(allegatoHome.getInstance().getId()); dossierHome.update(); allegatoHome.remove(); } return null; } }
Thank you in advance for any help,
Fausto