1 2 Previous Next 27 Replies Latest reply on Jun 4, 2008 9:56 PM by nickarls

    s:selectItems and s:convertEntity

    stereoscope

      Hi!


      i tried to put in a component into my jsf page! suddenly many obstacles came into my way. after a while i figured out how to come along with this issue but i have some questions.



      well i have an entity representing a location and another entity which describes a locationType and the address entity  holding the address for the specific location.


      @Entity
      public class Location {
      
           private Long id;
           private String name;
           private Address address;
           private String description;
           private LocationType locationType;
      
              @OneToOne(cascade = CascadeType.ALL)
              @JoinColumn(name="address_fk")
           public Address getAddress() {
                return address;
           }
              @OneToOne
              @JoinColumn(name="locationType_fk")
           public LocationType getLocationType() {
                return locationType;
           }
      





      @Entity
      public class LocationType {
      
           private Long id;
           private Location location;
           private String locationType_de;
           private String locationType_en;
      
              @OneToOne(mappedBy = "locationType")
              public Location getLocation() {
              return location;
              }
      



      @Entity
      public class Address {
      
          private Long id;
          private String countryNameCode;
          private String administrativeAreaName;
          private String subAdministrativeAreaName;
          private String localityName;
          private String thoroughfareName;
          private String postalCodeNumber;
          private GLatLng coordinates;
          private User user;
          private Location location;
      
          @OneToOne(mappedBy = "address")
          public User getUser() {
           return user;
          }
           
          @OneToOne(mappedBy = "address")
          public Location getLocation() {
           return location;
          }
      



      furthermore i have written down a form and i want to display the different types of locations in a dropdown box. this actually works. i did this way that i fill a list with locationtype objects and add the objects attributes either englisch or german to a list of the type SelectItems to pass it to the page...


      but i dont know how to merge the chosen value from the selectonemenu back to an object.


      or is there an easier way!?



      public ArrayList<SelectItem> getLocationType() {
      
                types = em.createQuery("Select type from LocationType type").getResultList();
      
                for (LocationType loc : types) {
                     locationType.add(new SelectItem(loc.getLocationType_de()));
                }          
                return locationType;          
           }





      anybody an idea!?

        • 1. Re: s:selectItems and s:convertEntity
          stereoscope

          HELP!

          • 2. Re: s:selectItems and s:convertEntity
            umajeric

            Try this:


            public List<LocationType> getLocationType() {
                      return em.createQuery("Select type from LocationType type").getResultList();     
                 }


            • 3. Re: s:selectItems and s:convertEntity
              umajeric

              And then:


              <h:selectOneMenu value="#{location.locationType}" >
                   <s:selectItems value="#{locationType}" var="type" label="#{type.locationType_de}" />
                   <s:convertEntity/>
              </h:selectOneMenu>


              • 4. Re: s:selectItems and s:convertEntity
                stereoscope

                hi!


                thanks for you message! i tried out your suggestion but i had to realize that s:selectItems requires a list of objects of the type SelectItem!


                Is there any other possibility to come along with this issue!?


                greetings

                • 5. Re: s:selectItems and s:convertEntity
                  nickarls

                  Behold, the s:convertEntity


                  (remember to override your equals() and hashCode() in the entity)

                  • 6. Re: s:selectItems and s:convertEntity

                    s:selectItem does not only take a list of selectItems.


                    You may have a List<Person> with id/name as fields in entity Person and then just do the following:


                    <h:selectOneMenu value="#{bean.chosenPerson}" >
                         <s:selectItems value="#{bean.people}" var="person" label="#{person.name}" />
                         <s:convertEntity/>
                    </h:selectOneMenu>
                    



                    And chosen person will be assigned to bean.chosenPerson on submit.


                    I use war in jboss4.2.2 and have often added this to components.xml. I have forgotten why though... might be because I named the EntityManager em and not entityManager.


                    <component name="org.jboss.seam.ui.EntityConverter">
                      <property name="entityManager">#{em}</property>
                    </component>

                    • 7. Re: s:selectItems and s:convertEntity
                      stereoscope

                      ufffffff!??


                      well...override? hashcode equals...! why ? is there any example or documentation out there dealing with this stuff.!


                      hehe ... didnt't get your point ... sorry!


                      cheers

                      • 8. Re: s:selectItems and s:convertEntity

                        There are probably thousands of users who have got this working.
                        Search the source example there are a few:


                        daniel@daniel-linux:~/testing/seam-trunk/examples$ grep -i -r convertentity * | grep -v svn | cut -d':' -f1
                        dvdstore/view/WEB-INF/incl/searchbox.xhtml
                        seambay/view/search.xhtml            
                        seambay/view/buy.xhtml               
                        seamdiscs/view/disc.xhtml   
                        
                        



                        I have removed some uninteresting hits. In other words, look in the  dvdstore, seambay, seamdiscs samples.

                        • 9. Re: s:selectItems and s:convertEntity
                          stereoscope

                          hmm..


                          i tried this one out and it worked. the menu is filled now with the right values! but if i try to submit the form in which the menu is integrated i get an error on the server side that the posted value of the menu is invalid!


                          of course if the type of the selectitem is e.g. LocationType
                          the value passed by the selectonemenu must be LocationType at the serverside!


                          ....

                          • 10. Re: s:selectItems and s:convertEntity
                            nickarls

                            Sound like a classic hashCode(), equals() issue, post your entity

                            • 11. Re: s:selectItems and s:convertEntity
                              stereoscope

                              in the first entry of this site are the entities!


                              hear is the select menu from the page



                              <s:decorate id="typeDecorate" template="/layout/edit.xhtml">
                                 <ui:define name="label">#{messages['location.type']}</ui:define> 
                                     <h:selectOneMenu id="selectLocation" value="{locationBean.type}">
                                        <s:selectItems value="#{locationBean.locationType}" 
                                                       var="type" 
                                                    label="#{type.locationType_de}" 
                                                    noSelectionLabel="#{messages['location.select']}"/>
                                        <s:convertEntity/>
                                      </h:selectOneMenu>
                              </s:decorate>




                              @PersistenceContext
                              private EntityManager em;
                              
                              private LocationType type;
                              
                              public List<LocationType> getLocationType() {
                                        
                                        List<LocationType> list =  em.createQuery("Select type from LocationType type").getResultList();
                                        return list;
                              }
                              
                              public LocationType getType() {
                                        return type;
                              }
                              
                              public void setType(LocationType type) {
                                        this.type = type;
                              }
                              




                              thats it there isn't more!

                              • 12. Re: s:selectItems and s:convertEntity
                                nickarls

                                Try using a seam-managed persistence context.

                                • 13. Re: s:selectItems and s:convertEntity
                                  stereoscope

                                  well...ain't im using this persintence context if i inject the persistence context into my bean !?

                                  • 14. Re: s:selectItems and s:convertEntity
                                    nickarls

                                    No, it's injected with @In. Show your components.xml

                                    1 2 Previous Next