1 Reply Latest reply on Aug 14, 2007 4:40 AM by Enrico Gulfi

    DataModel entries appearing twice

    Enrico Gulfi Newbie

      I'm writing simple CRUD application that handles I'm writing simple CRUD application that handles "folders" and associated "items". The list of all folders is displayed on the home page as following:

      <h:dataTable value="#{folders}" var="_folder">
       <h:column>
       <f:facet name="header">Id</f:facet>
       <s:link action="#{folderFinder.selectFolder}" value="#{_folder.id}"/>
       </h:column>
       <h:column>
       <f:facet name="header">Title</f:facet>
       <h:outputText value="#{_folder.title}" />
       </h:column>
       ?
      </h:dataTable>
      

      "folders" are managed in a SFSB:
       ?
       @DataModel("folders")
       List<Folder> folders;
      
       @DataModelSelection
       @Out(required=false)
       Folder folder;
      
       @In(create=true) @Out(required=false)
       transient ItemFinder itemFinder;
      
       @Out(required=false)
       List<Item> item;
      
       @Factory("folders")
       public void refresh() {
       folders = em.createQuery("from Folder").getResultList();
       }
      
       @Begin(join=true)
       public String selectFolder() {
       itemFinder.refresh(folder); //init "items" to contain items from this folder
       return "view_folder";
       }
       ?
      


      So far so good. My problem starts when I select a folder and display it with its items as every item appears twice in the dataTable! A folder is displayed as following :

      <t:panelTabbedPane ? >
       <t:panelTab label="Folder">
       <h:panelGrid columns="2">
       <h:outputText value="Id"/>
       <h:outputText value="#{folder.id}"/>
       <h:outputText value="#Title"/>
       <h:outputText value="#{folder.title}"/>
       ?
       </h:panelGrid>
       </t:panelTab>
      
       <t:panelTab label="Items">
       <h:dataTable value="#{items}" var="_item" >
       <h:column>
       <f:facet name="header">Item id</f:facet>
       <s:link action="#{itemFinder.selectItem}" value="#{_item.id}"/>
       </h:column>
       <h:column>
       <f:facet name="header">Type</f:facet>
       <h:outputText value="#{_item.type}" />
       </h:column>
       ?
       </h:dataTable>
       </t:panelTab>
      </t:panelTabbedPane>
      


      "itemFinder" is a SFSB the manages "items":

       @DataModel("items")
       List<Item> items;
      
       @DataModelSelection
       @Out(required=false)
       Item item;
      
       public void refresh(Folder folder) {
       items = folder.getItems();
       }
      


      Needless to say the database contains only one copy of each item.
      I?m using Seam 1.2.1, AS 4.0.5, MySQL 5.0 and mysql-connector-3.1.13.

      I would appreciate if anybody could point me in the right direction.

      Thanks in advance,

      Enrico


        • 1. Re: DataModel entries appearing twice
          Enrico Gulfi Newbie

          Problem solved: trying to be smart the Item.setFolder method was implemented as following:

          public void setFolder(Folder folder) {
           this.folder = folder;
           folder.getItems().add(this);
          }

          In reality item.setFolder and folder.getItems().add are called as necessary by the framework when loading objects from the db. The above implementation o f setFolder adds every item a second time resulting in a list containg every item twice. Removing the
          folder.getItems().add(this);

          line solved the problem :-)