3 Replies Latest reply on Mar 31, 2007 11:39 PM by Norman Richards

    EntityConverter - when and how?

    ir vega Newbie

      Is there a layman's (but accurate and clear) description of what entity converters are for and how to use them.

      I read everything google threw at me (search string: seam convertEntity) including
      but am none the more certain about the precise intention.

      Is it to convert an id (selected by the user from dropdown list, for example) to a full blown entity? Do you have an example use-case (in English, not code)?

        • 1. Re: EntityConverter - when and how?
          Norman Richards Master

          The entity converter is a JSF converter for entities. If you don't understand what a JSF converter is, you should take a look at a good JSF book.

          The main use case I've found for entity converter is to provide a string-object mapping for use in menus and dropdowns. The DVD store uses it in the search box to allow the user to select a search category.

          • 2. Re: EntityConverter - when and how?
            ir vega Newbie

            OK, so the catbean.categories in

            <h:selectOneMenu value="#{search.category}"
             <f:selectItem itemLabel="Any" itemValue="#{catbean.nullCategory}" />
             <f:selectItems value="#{catbean.categories}" />

            returns the map populated in the CategotyBean at instantiation.
            When the converter is called into action, it takes the key (the selected Category's name), now in the appropriate context, and looks up the actual Category object (in the same map) to get that Category's id for use in the query.
            Is that a better way to achieve this effect than the use of something like an entityQuery for retrieving the Categorys (categoriesQuery) with the following in the components.xml ?
            <h:selectOneListbox <h:selectOneMenu value="#{search.category}"
             <s:selectItems value="#{categoriesQuery.resultList}" var="category" label="#{category.name}" />

            Would a converter be needed in this case? What are the pros and cons of each approach (assuming my suggestion would even work - haven't tried it yet)?

            • 3. Re: EntityConverter - when and how?
              Norman Richards Master

              No, it would look like this:

              <h:selectOneMenu value="#{search.category}">
               <s:convertEntity />
               <s:selectItems value="#{allCategories.resultList}" var="category"
               noSelectionLabel="ANY" />