title: Conversion Error setting value for 'null Converter' - Why do I need a Converter in JSF?
body: <p>I have problems understanding how to use selection in JSF 2 with POJO/entity effectively. For example, I'm trying to select a <code>Warehouse</code> entity via the below dropdown:</p>
  
  <pre><code>&lt;h:selectOneMenu value="#{bean.selectedWarehouse}"&gt;
      &lt;f:selectItem itemLabel="Choose one .." itemValue="#{null}" /&gt;
      &lt;f:selectItems value="#{bean.availableWarehouses}" /&gt;
  &lt;/h:selectOneMenu&gt;
  </code></pre>
  
  <p>And the below managed bean:</p>
  
  <pre><code>@Named
  @ViewScoped
  public class Bean {
  
      private Warehouse selectedWarehouse;
      private List&lt;SelectItem&gt; availableWarehouses;
  
      // ...
  
      @PostConstruct
      public void init() {
          // ...
  
          availableWarehouses = new ArrayList&lt;&gt;();
  
          for (Warehouse warehouse : warehouseService.listAll()) {
              availableWarehouses.add(new SelectItem(warehouse, warehouse.getName()));
          }
      }
  
      // ...
  }
  </code></pre>
  
  <p>Notice that I use the whole <code>Warehouse</code> entity as the value of <code>SelectItem</code>.</p>
  
  <p>When I submit the form, this fails with the following faces message:</p>
  
  <blockquote>
    <p>Conversion Error setting value 'com.example.Warehouse@cafebabe' for 'null Converter'.</p>
  </blockquote>
  
  <p>I was hoping that JSF could just set the correct <code>Warehouse</code> object to my managed bean when I wrap it in a <code>SelectItem</code>. Wrapping my entity inside the <code>SelectItem</code> was meant to skip creating a <code>Converter</code> for my entity. </p>
  
  <p>Do I really have to use a <code>Converter</code> whenever I want to make use of entities in my <code>&lt;h:selectOneMenu&gt;</code>? It should for JSF be possible to just extract the selected item from the list of available items. If I really have to use a converter, what is the practical way of doing it? So far I came up to this:</p>
  
  <ol>
  <li>Create a <code>Converter</code> implementation for the entity.</li>
  <li>Overriding the <code>getAsString()</code>. I think I don't need this since the label property of the <code>SelectItem</code> will be used to display the dropdown option label.</li>
  <li>Overriding the <code>getAsObject()</code>. I think this will be used to return the correct <code>SelectItem</code> or entity depending on the type of the selected field defined in the managed bean. </li>
  </ol>
  
  <p>The <code>getAsObject()</code> confuses me. What is the efficient way to do this? Having the string value, how do I get the associated entity object? Should I query the entity object from the service object based on the string value and return the entity? Or perhaps somehow I can access the list of the entities that form the selection items, loop them to find the correct entity, and return the entity?</p>
  
  <p>What is the normal approach of this?</p>