1 Reply Latest reply on Aug 31, 2008 11:43 AM by Pete Muir

    doubts on s:selectItems + s:convertEntity + FetchType.EAGER:

    Nicola Ben Novice

      Hi,
      I had a problem with s:selectItems combobox in some of my forms. I use seam 2.1.0.A1 and openJDK6.
      After submission (still inside a conversation) I could see my form with the right list value. But once out of the conversation, entering again the form, the list value was not anymore bind to the persisted value.
      Here some code to better understand:


      CompanyEdit.xhtml:


      <h:selectOneMenu id="activity" required="true" value="#{companyHome.instance.activity}">
           <s:selectItems value="#{listsCache.activities}" var="ac"
           label="#{ac.name}" hideNoSelection="true" noSelectionLabel="Please select a value..." />
           <s:convertEntity/>
      </h:selectOneMenu>



      Company.java:


      @Entity
      @Name("company")
      public class Company extends BaseEntity {
      ...
      @ManyToOne(fetch = FetchType.EAGER)
          @JoinColumn(name = "id_activity")
          public Activity getActivity() {
              return activity;
          }
      
          public void setActivity(Activity activity) {
              this.activity = activity;
          }
      ...
      



      In BaseEntity.java (superclass of all entities, implementing Ided interface) I have equals and hashCode:


      @Override
          public boolean equals(Object obj) {
           if(obj == null){
               return false;
           }
           Ided that;
           try {
               that = (Ided) obj;
           } catch (ClassCastException e) {
               return false;
           }     
           if (!this.getClass().equals(that.getClass())){
               return false;
           }     
           if(getId() == 0 || that.getId() == 0){
               return false;
           }
           return that.getId() == getId();
          }
      
          @Override
          public int hashCode() {
           return Long.valueOf(getId()).hashCode();
          }




      I read some post about selectItems and convertEntity problems:


      1) I get Value is not a valid option when using s:selectItems and s:convertEntity, or a custom converter


      2) Entity Converter - value is not valid


      So I try to:


      1) Override equals and hashCode inside the entity


      2) Use conversation scope for beans and entities


      3) Use only one persistence context, called entityManager


      And finally I solved my problem only modifying the FetchType attribute in Company.java: from LAZY I changed into EAGER (I saw a post that suggested it).


      But I'd like to better understand the reason of this workaround.
      Could you please help?


      Thank you,
      N