title: JSF 2.0 - selectOneMenu defaults to 0 when empty
body: <p>For my project i want to implement a combobox with <strong>h:selectOneMenu</strong>. I read many posts about how to handle the selectOneMenu properly and how to implement a custom converter. The combobox shall store the ID (<strong>Long</strong>) from a different DB Table (named fk_ID) for which a constrain is in place. So far everything seems to function as planned. Now to my problem:</p>
  
  <p>The column fk_ID is optional, so the first option in the combobox is a <strong>f:selectItem</strong> with no value labeled "choose ...". When I save the form and change the value everything is OK, except if I set it back to "choose ...". The error I get is a <strong>constraint integrity violation</strong>.</p>
  
  <p>I found the problem too: The converter returns <strong>null</strong> as intended but the bean receives the <strong>Long 0</strong>.</p>
  
  <p>The XHTML:</p>
  
  <pre><code>&lt;h:selectOneMenu value="#{userBean.selectedModel.fk_id}"
      id="combobox"&gt;
      &lt;f:selectItem itemLabel="choose ..." /&gt;
      &lt;f:selectItems
          value="#{userBean.items}"
          var="item"
          itemLabel="#{item.value}"
          itemValue="#{item.id}"
      /&gt;
      &lt;f:converter converterId="userConverter" /&gt;
  &lt;/h:selectOneMenu&gt;
  </code></pre>
  
  <p>The custom converter:</p>
  
  <pre><code>@FacesConverter(value = "userConverter")
  public class UserConverter implements Converter {
      @Override
      public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
          if (arg2 != null &amp;&amp; !arg2.equals("null"))
              return Long.valueOf(arg2);
  
          return null;
      }
      @Override
      public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
          return String.valueOf(arg2);
      }
  }
  </code></pre>
  
  <p>The bean (very short version):</p>
  
  <pre><code>@Named(value = "userBean")
  public class UserController implements Serializable {
  
      @Getter
      @Setter
      private UserModel selectedModel;
  
      @Getter
      @Setter
      private List&lt;UserModel&gt; items;
  
      public void saveModel() {
          selectedModel = userService.saveModel(selectedModel);
      }
  }
  </code></pre>
  
  <p>The problem is that the <strong>fk_id</strong> is <strong>Long 0</strong>, but should be <strong>null</strong>. I can solve the problem by writing</p>
  
  <pre><code>if (selectedModel.getFk_id() == 0)
      selectedModel.setFk_id(null);
  </code></pre>
  
  <p>, but thats not a solution I am comfortable with.</p>
  
  <p>My Question: Why is the value <strong>null</strong>, which the converter returns, the <strong>Long 0</strong> inside the <strong>saveModel()</strong> method?</p>