1 Reply Latest reply on Jan 26, 2009 9:44 AM by Tony Herstell

    seam, richfaces pickList and equals

    Tony Herstell Master

      This was simple....


      <rich:pickList value="#{event.sponsorDetails.sponsors}"> 
           <s:selectItems value="#{availableUsers}" var="eachUser" label="#{eachUser.surname} , #{eachUser.firstname}"/>
           <s:convertEntity/>
      </rich:pickList>
      



      Or so I thought...



          @Out(value="availableUsers",required=true)
          private List<User> availableUsers = new ArrayList<User>();
      





           
              /* (non-Javadoc)
            * @see nz.co.selwynequestriancentre.action.eventsManagement.EventsManagementController#findUsers()
            */
           @Factory("availableUsers")
           @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
           @Override
           public void findUsers()
           {
                if (availableUsers.isEmpty()) {
                     availableUsers = (List<User>)entityManager.createQuery("from User u order by u.surname asc").getResultList();
                }
           }
      




      simply does not cut it!


      You get a very strange error from JSF...




      Validation Error: Value is not valid


      What this eventually came to (possibly) is that you need to override the default equals method!


      Right or Wrong I used:



          @Override
          public boolean equals(Object o) {
               return this.getId() == ((User)o).getId();
          }
      



      This now appaered to work until I persisted the containing entity and I got a detachedEntityException.. ARGH!


      So had to add:



      List<User> sponsorsToAdd = new ArrayList<User>();
                for (User eachSponsor : this.event.getSponsorDetails().getSponsors()) {
                     sponsorsToAdd.add(entityManager.merge(eachSponsor)); // Re-Attach the Sponsor object.
                }
      



      This seems a very big workaround to get this to work... is this kind of feature being addressed in JSF2?



      As I have not actually persisted this yet due to other problems I still have not seen this work, but feel confident.