title: Using SelectManyCheckbox with list of objects
body: <p>Im trying to create a JSF page that lets the user select X amount of ingredients, and then saves those selected ingredients to a list.</p>
  
  <p>Ingredient is an object with two values, String IngredientName and int ingredientPrice.</p>
  
  <p>What I want to do is to create 1 selectItem per Ingredient in an IngredientList (dynamically sized), and then save the selected items to another list of ingredients.</p>
  
  <p>I've tried doing this multiple different ways but either I get classcast exceptions or the checkboxes don't appear at all.</p>
  
  <p>My Bean:</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class ManagedIngredientsBean {
  
  @EJB
  IngredientBean iBean;
  
  private List&lt;Ingredient&gt; ingredientList;
  private List&lt;Ingredient&gt; checkedOptions;
  private List&lt;SelectItem&gt; selectList;
  
  public ManagedIngredientsBean() {
  
  }
  
  
  public String createNew(){
  
      ingredientList = iBean.getAllIngredients();
      selectList = new ArrayList&lt;SelectItem&gt;(ingredientList.size());
      for(Ingredient i : ingredientList){
          selectList.add(new SelectItem(i.getIngredientName()));
      }
  
      return "createnew.xhtml";
  }
  
  public List&lt;SelectItem&gt; getSelectList() {
      return selectList;
  }
  
  public void setSelectList(List&lt;SelectItem&gt; selectList) {
      this.selectList = selectList;
  }
  
  public List&lt;Ingredient&gt; getCheckedOptions() {
      return checkedOptions;
  }
  
  public void setCheckedOptions(List&lt;Ingredient&gt; checkedOptions) {
      this.checkedOptions = checkedOptions;
  }
  
  public List&lt;Ingredient&gt; getIngredientList() {
      return ingredientList;
  }
  
  public void setIngredientList(List&lt;Ingredient&gt; ingredientList) {
      this.ingredientList = ingredientList;
  }
  
  @FacesConverter(value="userConverter")
  public static class UserConverter implements Converter {
      public Object getAsObject(FacesContext facesContext,
                                UIComponent component, String value) {
          return value;
      }
  
      public String getAsString(FacesContext facesContext,
                                UIComponent component, Object o) {
          Ingredient i = (Ingredient) o;
          return i.getIngredientName();
  
      }
  }
  }
  </code></pre>
  
  <p>IngredientBean used to get the Ingredient items from the persistence database and returning them as a list:</p>
  
  <pre><code>@Stateless(name = "IngredientEJB")
  public class IngredientBean {
  
      EntityManagerFactory entFactory;
      EntityManager em;
  
      public IngredientBean() {
          entFactory = Persistence.createEntityManagerFactory("NewPersistenceUnit");
          em = entFactory.createEntityManager();
      }
  
      public List&lt;Ingredient&gt; getAllIngredients(){
          TypedQuery&lt;Ingredient&gt; ingQuery = em.createQuery("SELECT i FROM Ingredient i", Ingredient.class);
          List&lt;Ingredient&gt; iList =  ingQuery.getResultList();
  
          return iList;
  
      }
  }
  </code></pre>
  
  <p>My JSF Page:</p>
  
  <pre><code>&lt;!DOCTYPE html
          PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://xmlns.jcp.org/jsf/core"&gt;
  
  &lt;h:head&gt;
      &lt;title&gt;Create New Order&lt;/title&gt;
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
      &lt;h:form&gt;
          &lt;h:selectManyCheckbox value = "#{managedIngredientsBean.checkedOptions}"&gt;
                  &lt;f:converter converterId="userConverter"/&gt;
                  &lt;f:selectItem value = "#{managedIngredientsBean.selectList}" var = "item" itemLabel = "#{item.getIngredientName()}" itemValue = "#{item}"/&gt;
          &lt;/h:selectManyCheckbox&gt;
      &lt;/h:form&gt;
  
  &lt;/h:body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>I'm probably missing something obvious or simply misunderstanding how to use the selectManyCheckbox element but I'm completely stuck on how to fix this. Appreciate any answers on how I should be implementing this. :)</p>
  
  <p>Edit: Forgot to mention, the createNew() method in the managed bean is called in the previous JSF page and redirects to this one.</p>