5 Replies Latest reply on Oct 8, 2008 8:13 PM by kstoneman

    a problem with SelectOneMenu

    lcbdl888

      Hi,


      I have a SelectOneMenu on my page. Everything is fine, except the default selected value of the menu is not the value in the database when I load the page. It is always null (Not Selected). Anybody can tell me why?


      Thanks


      <h:selectOneMenu id="service1" value="#{personHome.instance.serviceAssistant1}">
           <s:selectItems value="#{codeTableManager.codeServiceAssistants}" var="sa"
           label="#{sa.fullName}" noSelectionLabel="Not Selected"/>
           <s:convertEntity />
           <a:support reRender="service1Email" event="onchange"/>
      </h:selectOneMenu>



      @Name("codeTableManager")
      @AutoCreate
      public class CodeTableManager {
      
           @In
           private EntityManager entityManager;
      
           @In
           CustomIdentity identity;
      
           @SuppressWarnings("unchecked")
           public List<CodeMarketingOffice> getCodeMarketingOffices() {
                return entityManager.createQuery("select office from CodeMarketingOffice office").getResultList();
           }
      
           @SuppressWarnings("unchecked")
           public List<CodeMarketingRep> getCodeMarketingReps() {
                return entityManager.createQuery("select mr from CodeMarketingRep mr").getResultList();
           }
      
           @SuppressWarnings("unchecked")
           public List<CodeMarketingRep> getCodeServiceAssistants() {
                return entityManager.createQuery("select sa from CodeServiceAssistant sa").getResultList();
           }
      }



      @Entity
      @Table(name="PERSON", uniqueConstraints=
          @UniqueConstraint(columnNames={"USERID_TXT"}))
      public class Person {
           
           public enum Status {ACTIVE, INACTIVE, NOT_IN_LDAP}
      
           @Id
           @GeneratedValue
           @Column(name="PERSON_ID")
           private Long id;
           
           @Column(name="FIRST_NAME_TXT", length=40)
           @NotNull
           private String firstName;
           
           @Column(name="LAST_NAME_TXT", length=40)
           @NotNull
           private String lastName;
           
           @Column(name="PRIMARY_PHONE_TXT", length=40)
           @PhoneValid
           private String primaryPhone;
           
           @Column(name="SECONDARY_PHONE_TXT", length=40)
           @PhoneValid
           private String secondaryPhone;
           
           @Column(name="WORK_EXTENSION_TXT", length=5)
           private String workExtension;
           
           @Column(name="USERID_TXT", length=10, unique=true)
           private String userId;
           
           @Column(name="EMAIL_ADDRESS_TXT", length=100)
           @Email
           private String emailAddress;
           
           @Column(name="STATUS_CODE", length=10)
           @Enumerated(EnumType.STRING)
           private Status status = Status.ACTIVE;
           
           @Temporal(TemporalType.TIMESTAMP)
           @Column(name="LAST_LDAP_SYNC_TS")
           private Date lastLDAPSynch;
           
           @Column(name="EMAIL_REMINDER_IND", nullable=false)
           @NotNull
           @Type(type="yes_no")
           private boolean acceptEmail = true;
           
           @Column(name = "PRODUCER_NAME", length = 40)
           private String producerName;
      
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "MARKETING_REP_ID")
           private CodeMarketingRep marketingRep;
           
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "SERVICE_ASSISTANT_ID1")
           private CodeServiceAssistant serviceAssistant1;
           
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "SERVICE_ASSISTANT_ID2")
           private CodeServiceAssistant serviceAssistant2;
           
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "OFFICE_ID")
           private CodeMarketingOffice marketingOffice;
           
           @Version
           @Column(name="VERSION_NUM")
           private Long version;
      
           public Long getId() {
                return id;
           }
              
              getters and setters ......
      


        • 1. Re: a problem with SelectOneMenu
          lcbdl888

          Finally, I figured out by myself. The answer is we can't use customized equals() method. Can the development team tell us why?


          @Entity
          @Table(name="CODE_MARKETING_OFFICE")
          public class CodeMarketingOffice {
               
               @Id
               @Column(name="OFFICE_ID")
               @GeneratedValue
               private Long id;
               
               @Column(name="OFFICE_NAME", length=50, nullable=false)
               @NotNull
               private String officeName;
          
                  Getters and setters ...
          
               @Override
               public int hashCode() {
                    final int prime = 31;
                    int result = 1;
                    result = prime * result + ((id == null) ? 0 : id.hashCode());
                    return result;
               }
          
          
          //     @Override
          //     public boolean equals(Object obj) {
          //          if (this == obj)
          //               return true;
          //          if (obj == null)
          //               return false;
          //          if (!(obj instanceof CodeMarketingOffice))
          //               return false;
          //          final CodeMarketingOffice other = (CodeMarketingOffice) obj;
          //          if (id == null) {
          //               if (other.id != null)
          //                    return false;
          //          } else if (!id.equals(other.id))
          //               return false;
          //          return true;
          //     }
               
          }

          • 2. Re: a problem with SelectOneMenu
            sjmenden

            because the id is not known if the entity is not persisted/attached

            • 3. Re: a problem with SelectOneMenu
              lcbdl888

              It might be caused by Hibernate proxy.  I have an experience when I debug an application and couldn't read the property values of a hibernate entity. I have to call the getter method to see the value.


              In the above equals method, it will fix the problem if I changed to use

              other.getId()

              other than
              other.id



              The correct code should be


                  if (id == null) {
                      if (other.id != null)
                          return false;
                   } else if (!id.equals(other.id))
                      return false;
              

              • 4. Re: a problem with SelectOneMenu
                lcbdl888

                Sorry, the correct code should be


                    if (id == null) {
                        if (other.getId() != null)
                            return false;
                     } else if (!id.equals(other.getId()))
                        return false;
                

                • 5. Re: a problem with SelectOneMenu
                  kstoneman
                  This is not the same subject, but I am also stuck on a drop down menu. I am trying to populate a drop down with data from a database table.

                  When I use this:

                  <h:selectOneMenu id="rolename" styleClass="text" value="#{rbacRolesList.rbacRoles.rolename}">
                  <s:selectItems value="#{roleNameList.resultList}" var="roleName" label="#{roleName.description}"noSelectionLabel="None"/>
                  </h:selectOneMenu>

                  I get just the "None" in the drop down box.  Almost like the query is returning no results?  I found somewhere that I have to override the toString() method on my item or my values will all be memory pointers (the default toString in the base object class).  Problem is I am not sure where and how to make that change.


                  Any ideas? Or could you point me in the right direction?
                  Thanks.