7 Replies Latest reply on Jul 12, 2011 1:04 PM by ruthlesset

    Error looping through a Set

    ruthlesset
      I am just on my first month still trying to learn SEAM and am having a hard time figuring out this error. I have two tables with the primary key of the table M_Module mapped to the foreign key module_recid of the table M_Function. When I try to loop through the functions of a module I get an error. What am I doing wrong?

      MModule.java

      package com.demo.model;
      // Generated Mar 9, 2011 10:27:42 AM by Hibernate Tools 3.2.4.GA

      import java.util.Date;
      import java.util.HashSet;
      import java.util.Set;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;

      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;


      @Entity
      @Table(name = "M_MODULE")
      @SequenceGenerator(name = "moduleSeq", sequenceName = "M_MODULE_SEQ")
      public class MModule implements java.io.Serializable {

              private static final long serialVersionUID = 1L;
              private Integer recid;
              private String name;
          private Set<MFunction> MFunctions = new HashSet<MFunction>(0);

              public MModule() {
              }

              public MModule(Integer recid, String name) {
                      this.recid = recid;
                      this.name = name;
              }

              public MModule(Integer recid, String name,
                              Set<MFunction> MFunctions) {
                      this.recid = recid;
                      this.name = name;
                      this.MFunctions = MFunctions;
              }

              @Id
              @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "moduleSeq")
              @Column(name = "RECID", unique = true, nullable = false, precision = 5, scale = 0)
              public Integer getRecid() {
                      return this.recid;
              }
              public void setRecid(Integer recid) {
                      this.recid = recid;
              }

              @Column(name = "NAME", nullable = false, length = 80)
              @NotNull
              @Length(max = 80)
              public String getName() {
                      return this.name;
              }

              public void setName(String name) {
                      this.name = name;
              }

              @OneToMany(fetch=FetchType.LAZY, mappedBy="MModule")
          public Set<MFunction> getMFunctions() {
              return this.MFunctions;
          }
         
          public void setMFunctions(Set<MFunction> MFunctions) {
              this.MFunctions = MFunctions;
          }

      }


      --------------------------------------------------------------------------------------------------------------
      MModuleHome.java


      package com.demo.action;

      import java.util.ArrayList;
      import java.util.List;

      import javax.persistence.EntityManager;

      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;

      import com.demo.model.MFunction;
      import com.demo.model.MModule;

      @Name("mModuleHome")
      public class MModuleHome extends EntityHome<MModule> {

              private static final long serialVersionUID = 1L;

              @In private EntityManager entityManager;

              public void setmModuleRecid(Integer id) {
                      setId(id);
              }

              public Integer getmModuleRecid() {
                      return (Integer) getId();
              }

              @Override
              protected MModule createInstance() {
                      MModule mModule = new MModule();
                      return mModule;
              }

              public void load() {
                      if (isIdDefined()) {
                              wire();
                      }
              }

              public void wire() {
                      getInstance();
              }

              public boolean isWired() {
                      return true;
              }

              public MModule getDefinedInstance() {
                      return isIdDefined() ? getInstance() : null;
              }

              public List<MFunction> getMFunctions() {
                      return getInstance() == null ? null : new ArrayList<MFunction>(
                                      getInstance().getMFunctions());
              }

      }



      --------------------------------------------------------------------------------------------------------------
      MFunction.java


      package com.demo.model;
      // Generated Mar 9, 2011 10:27:42 AM by Hibernate Tools 3.2.4.GA

      import java.util.Date;
      import java.util.HashSet;
      import java.util.Set;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;

      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;

      /**
      * MModule generated by hbm2java
      */
      @Entity
      @Table(name = "M_FUNCTION")
      @SequenceGenerator(name = "functionSeq", sequenceName = "M_FUNCTION_SEQ")
      public class MFunction implements java.io.Serializable {

              private static final long serialVersionUID = 1L;
              private Integer recid;
          private MModule MModule;
              private String name;
              private Set<MContext> MContexts = new HashSet<MContext>(0);

              public MFunction() {
              }

              public MFunction(Integer recid, MModule MModule, String name) {
                      this.recid = recid;
              this.MModule = MModule;
                      this.name = name;
              }

              public MFunction(Integer recid, MModule MModule, String name,
                              Set<MContext> MContexts) {
                      this.recid = recid;
              this.MModule = MModule;
                      this.name = name;
                      this.MContexts = MContexts;
              }

              @Id
              @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "functionSeq")
              @Column(name = "RECID", unique = true, nullable = false, precision = 5, scale = 0)
              public Integer getRecid() {
                      return this.recid;
              }

              public void setRecid(Integer recid) {
                      this.recid = recid;
              }

              @ManyToOne(fetch=FetchType.LAZY)
          @JoinColumn(name="MODULE_RECID", nullable=false)
          @NotNull
          public MModule getMModule() {
              return this.MModule;
          }
         
          public void setMModule(MModule MModule) {
              this.MModule = MModule;
          }

              @Column(name = "NAME", nullable = false, length = 50)
              @NotNull
              @Length(max = 50)
              public String getName() {
                      return this.name;
              }

              public void setName(String name) {
                      this.name = name;
              }

              @OneToMany(fetch = FetchType.LAZY, mappedBy = "MFunction")
              public Set<MContext> getMContexts() {
                      return this.MContexts;
              }

              public void setMContexts(Set<MContext> MContexts) {
                      this.MContexts = MContexts;
              }

      }



      --------------------------------------------------------------------------------------------------------------
      MFunctionHome.java



      package com.demo.action;

      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;

      import javax.persistence.EntityManager;

      import org.jboss.seam.Component;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Transactional;
      import org.jboss.seam.framework.EntityHome;
      import org.jboss.seam.log.Log;

      import com.demo.model.MContext;
      import com.demo.model.MFunction;
      import com.demo.model.MModule;

      @Name("mFunctionHome")
      public class MFunctionHome extends EntityHome<MFunction> {

              private static final long serialVersionUID = 1L;

              @In(create = true)
              MModuleHome mModuleHome;

              @Logger private Log log;

              public void setmFunctionRecid(Integer id) {
                      setId(id);
              }

              public Integer getmFunctionRecid() {
                      return (Integer) getId();
              }

              @Override
              protected MFunction createInstance() {
                      MFunction mFunction = new MFunction();
                      return mFunction;
              }

              public void load() {
                      if (isIdDefined()) {
                              wire();
                      }
              }

              public void wire() {
                      getInstance();
                      MModule MModule = mModuleHome.getDefinedInstance();
                      if (MModule != null) {
                              getInstance().setMModule(MModule);
                      }
              }

              public boolean isWired() {
                      if (getInstance().getMModule() == null)
                              return false;
                      return true;
              }

              public MFunction getDefinedInstance() {
                      return isIdDefined() ? getInstance() : null;
              }

              public List<MContext> getMContexts() {
                      return getInstance() == null ? null : new ArrayList<MContext>(
                                      getInstance().getMContexts());
              }

      }



      --------------------------------------------------------------------------------------------------------------
      modules.xhtml


              <ui:repeat value="#{moduleList}" var="module">
                      <h:outputText value="[#{module.name}]" />
                      <ui:repeat value="#{module.MFunctions}" var="function">
                              <h:outputText value=" -- #{function.name} -- " />
                      </ui:repeat>
                      <h:outputText value="," />
              </ui:repeat>


      I get the error

              /modules.xhtml @75,53 value=" -- #{function.name} -- ": The class 'org.hibernate.collection.PersistentSet' does not have the property 'name'.
        • 1. Re: Error looping through a Set
          ruthlesset
          I missed one factory method that should go under MModuleHome.java

                  @SuppressWarnings("unchecked")
                  @Factory(value="moduleList",scope=ScopeType.EVENT)
                  public List<MModule> getModuleList() {
                          return entityManager.createQuery("SELECT mModule FROM MModule mModule ORDER BY mModule.name").getResultList();         
                  }
          • 2. Re: Error looping through a Set
            lvdberg

            Hi,


            Use a List (implementation to do the trick), Just put the values from the Set in an ArrayList:




            List<YourEntity> list = new ArryList<YourEntity>(yourEntitySet);
            
            ... Add the get and set..





            and use the list in your view instead of the set. To make it even better, you ould use a unmodifiable List.


            Leo


            • 3. Re: Error looping through a Set
              jseanjensen

              I had the same problem recently and solved it by like this:




              <rich:dataList var='user'
                   value="#{_parentObject.users.toArray()}">
                   <h:outputText value="#{user.displayName}" />
              </rich:dataList>



              This lets you leave the generated entity as a Set.  I'm not sure if one is preferred over the other but it does work.

              • 4. Re: Error looping through a Set
                ruthlesset

                Thank you so much for the quick response. Converting the Set to a ArrayList using .toArray() resolved the issue.

                • 5. Re: Error looping through a Set
                  ruthlesset
                  Another quick question regarding this:

                  Let us assume MFunction.java has another property displaySequence (which also exists in the database).

                  Now when I get the List of MFunction for a given MModule, I want it sorted out by the numeric value of displaySequence. MFunction already implements java.io.Serializable and hence I won't be able to implement Comparable<MFunction>.

                  What would be the best way to implement this?
                  • 6. Re: Error looping through a Set
                    lvdberg

                    Hi,





                    already implements java.io.Serializable and hence I won't be able to implement Comparable<MFunction>.



                    Your class can implement as many interfaces as needed.




                    public class MFunction implements Serializable, Comparable<MFunction>
                    ...
                    



                    Leo

                    • 7. Re: Error looping through a Set
                      ruthlesset

                      Leo van den Berg wrote on Jun 23, 2011 02:45:


                      Hi,




                      already implements java.io.Serializable and hence I won't be able to implement Comparable<MFunction>.



                      Your class can implement as many interfaces as needed.



                      public class MFunction implements Serializable, Comparable<MFunction>
                      ...
                      



                      Leo



                      Thanks.