7 Replies Latest reply on Jun 14, 2007 12:30 PM by andreigh.ts

    DataModel using Set does not work

    andreigh.ts

      Hi,

      I am trying to use java.util.Set as a @DataModel but it throws an exception (javax.faces.el.PropertyNotFoundException .... Bean: org.hibernate.collection.PersistentSet) when trying to display the xhtml.

      Replacing Set with List or array works very well.

      Here is the code:

      The Hibernate Entity bean that initially holds the set:

      @Entity
      @Table(name = "RFQ_Request", uniqueConstraints = {})
      public class RfqRequest implements java.io.Serializable {
      ...
       private Set<RfqRequestQuestion> rfqRequestQuestions = new HashSet<RfqRequestQuestion>(0);
      ...
      }
      


      One element of the Set looks like this:

      @Entity
      @Table(name = "RFQ_Question", uniqueConstraints = {})
      public class RfqQuestion implements java.io.Serializable {
      ...
       @Column(name = "Description", unique = false, nullable = false, insertable = true, updatable = true)
       public String getDescription() {
       return this.description;
       }
      ...
      }
      


      A seam component that should expose the DataModel:

      @Stateless
      @Name("requestWizardQuestions")
      public class RequestWizardQuestionsAction implements RequestWizardQuestions {
       @In(required=false, scope=ScopeType.CONVERSATION)
       @Out(required=false, scope=ScopeType.CONVERSATION)
       private RfqRequest rfqRequest;
      
       @DataModel
       public Set<RfqRequestQuestion> getRfqQuestions() {
       return rfqRequest.getRfqRequestQuestions();
       }
      
       @DataModelSelection
       private RfqRequestQuestion selectedQuestion;
      }
      


      And the page that should display the DataModel:

      ...
      <h:dataTable var="question" value="#{requestWizardQuestions.rfqQuestions}" >
       <h:column>
       <f:facet name="header">
       <h:outputText value="Question"/>
       </f:facet>
       <h:outputText value="#{question.description}"/>
       </h:column>
       </h:dataTable>
      ...
      


      This throws this exception when trying to display the page:

      javax.faces.el.PropertyNotFoundException: /requestWizard/questions.xhtml @24,59
      value="#{question.description}": Bean: org.hibernate.collection.PersistentSet, property: description
       at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:58)
       at javax.faces.component.UIOutput.getValue(UIOutput.java:77)
      .......
      


      If I change the @DataModel and replace the Set < RfqQuestion > with RfqQuestion[]. it works.
      What is wrong?

      Thanks

        • 1. Re: DataModel using Set does not work
          andreigh.ts

          Is there an example of how to use Set as a @DataModel ?

          • 2. Re: DataModel using Set does not work
            pmuir

            It should be

            <h:dataTable var="question" value="#{rfqQuestions}" >
            </h:dataTable>
            ...
            - you want to bind to the outjected variable, not the property of the bean.

            • 3. Re: DataModel using Set does not work
              andreigh.ts

              Thanks, it works. Now I understand, @DataModel outjects a brand new object with a new name (default variable name). This also means that the variable should be created with @Factory (usually).

              Now I don't understand why my initial approach worked with List and array :)

              • 4. Re: DataModel using Set does not work
                pmuir

                Because JSF supports sets and arrays natively, but not Sets - Seam does some trickery there.

                • 5. Re: DataModel using Set does not work
                  andreigh.ts

                  There is something weird how this @DataModel functions, maybe it can be documented somewhere.

                  In my example the @DataModel exposes the Set of a hibernate entity(from one-many relationship).

                  If the @DataModel is a property of a stateful bean (conversation scope), then modifying the hibernate set, automatically updates the DataModel (this means that somehow seam links the DataModel to the original object; it does not clone the collection?)

                  But if @DataModel is a property of a stateless bean, then modifying the hibernate set has no effect in the user interface.

                  I'm confused ... it's a bug?

                  • 6. Re: DataModel using Set does not work
                    gavin.king

                    Its to do with the default scoping rules. In the case of a SLSB, the datamodel defaults to PAGE context, which means gets serialized back and forth to the client.

                    • 7. Re: DataModel using Set does not work
                      andreigh.ts

                      Thank you all for the answers.

                      Can you please help me out - There still are some problems and searching on the forum seemsto be bugs, but I am not sure. Can you please confirm.

                      Here are the problems:

                      1) Stateful component - Conversation scope; outjecting Set DataModel (defaulting to Conversation scope).
                      Problem - UI is not refreshed, even though the Factory method is called on the add event.

                      @Stateful
                      @Scope(ScopeType.CONVERSATION)
                      @Name("requestWizardQuestions")
                      public class RequestWizardQuestionsAction implements RequestWizardQuestions {
                      ...
                       @DataModel
                       public Set<RfqRequestQuestion> requestQuestionsList;
                      
                       @DataModelSelection
                       @Out(required=false)
                       private RfqRequestQuestion selectedQuestion;
                      
                       @Factory("requestQuestionsList")
                       public void initQuestionsList() {
                       requestQuestionsList = rfqRequest.getRfqRequestQuestions());
                       }
                      
                       public void addNewQuestion() {
                       rfqRequest.getRfqRequestQuestions().add(new RfqRequestQuestion());
                       initQuestionsList();
                       }
                      ...
                      }
                      


                      Replacing Set with List works.
                      Is this a bug signaled here: http://www.jboss.com/index.html?module=bb&op=viewtopic&t=106232
                      ?

                      2) Stateful component - Conversation scope; outjecting DataModel to PAGE scope.
                      Problem - The @DataModelSelection is null (seems not to be injected) after clicking the item in UI.

                      @Stateful
                      @Scope(ScopeType.CONVERSATION)
                      @Name("requestWizardQuestions")
                      public class RequestWizardQuestionsAction implements RequestWizardQuestions {
                      ...
                       @DataModel(scope=ScopeType.PAGE)
                       public List<RfqRequestQuestion> requestQuestionsList;
                      
                       @DataModelSelection
                       @Out(required=false)
                       private RfqRequestQuestion selectedQuestion;
                      
                       @Factory("requestQuestionsList")
                       public void initQuestionsList() {
                       requestQuestionsList = new ArrayList<RfqRequestQuestion>(rfqRequest.getRfqRequestQuestions());
                       }
                      
                       public void deleteQuestion() {
                       rfqRequest.getRfqRequestQuestions().remove(selectedQuestion); entityManager.remove(selectedQuestion);
                       }
                      
                      ...
                      }
                      


                      deleteQuestion is called when clicking an item in the List, but selectedQuestion is null.

                      Is this the bug posted here:
                      http://jira.jboss.com/jira/browse/JBSEAM-308
                      ?

                      Am I doing something wrong, or are just 2 bugs?

                      Thanks