title: JSF SelectOneMenu get Entity
body: <p>I am trying to obtain a Customer entity from a JSF SelectOneMenu component: The classes I have are as follows:</p>
  
  <p>Customer Entity</p>
  
  <pre><code>@Entity
  @Table(name="CUSTOMERS")
  @NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
  public class Customer implements Serializable {
  
      @Id
      @GeneratedValue
      @Column(name="CUSTOMER_ID")
      private Long customerId;
  
      @Column(name="FIRST_NAME")
      private String firstName;
  
      @Column(name="LAST_NAME")
      private String lastName;
  
      private String email;
  
      public Long getCustomerId() {  
          return customerId;
      }
  
      public void setCustomerId(Long customerId) {
          this.customerId = customerId;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      @Override
      public String toString() {
          Long localCustomerId = customerId;
          String localFirstName = firstName;
          String localLastName = lastName;
          String localEmail = email;
          if (localCustomerId == null) {
              localCustomerId = 0L;
          }
          if (localEmail == null) {
              localEmail = "";
          }
          if (localFirstName == null) {
              localFirstName = "";
          }
          if (localLastName == null) {
              localLastName = "";
          }
  
          String toString = "customerId = " + localCustomerId + "\n";
          toString += "firstName = " + localFirstName + "\n";
          toString += "lastName = " + localLastName + "\n";
          toString += "email = " + localEmail;
  
          return toString;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Customer Controller</p>
  
  <pre><code> @ManagedBean
      @RequestScoped
      public class CustomerController implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Resource(name = "jdbc/__CustomerDBPool")
      private DataSource dataSource;
  
      @PersistenceUnit(unitName = "customerPersistenceUnit")
      private EntityManagerFactory emf;
  
      @Resource
      private UserTransaction userTransaction;
  
      @ManagedProperty(value = "#{customer}")
      private Customer customer;
  
  
      public DataSource getDataSource() {
          return dataSource;
      }
  
      public EntityManagerFactory getEmf() {
          return emf;
      }
  
      public void saveCustomer() {
          EntityManager entityManager = emf.createEntityManager();
          try {
              userTransaction.begin();
              entityManager.persist(customer);
              userTransaction.commit();
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      public void setEmf(EntityManagerFactory emf) {
          this.emf = emf;
      }
  
      public List&lt;Customer&gt; getCustomerList() {
  
          List&lt;Customer&gt; list = null;
          EntityManager em = emf.createEntityManager();
          try {
              Query q = em.createNamedQuery("Customer.findAll");
              int m = q.getResultList().size();
  
              list = (List&lt;Customer&gt;)  q.getResultList();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return list;
      }
  
      public Customer getCustomerById(Long id) {
          EntityManager entityManager = emf.createEntityManager();
          Query q = entityManager.createQuery("Select c FROM Customer c WHERE c.id = :id");
          q.setParameter("id", id);
          Customer c = (Customer) q.getResultList().get(q.getFirstResult());
          return c;
      }
  
      public String getCustomerDetails() {
          if (customer == null)
              return "NULL";
          else
              return customer.toString();
      }
  
      public Customer getCustomer() {
          return customer;
      }
  
      public void setCustomer(Customer customer) {
          this.customer = customer;
          //System.out.println(customer);
      }
      }
  </code></pre>
  
  <p>CustomerConverter</p>
  
  <pre><code>@ManagedBean
  @RequestScoped
  @FacesConverter(value = "CustomerConverter")
      public class CustomerConverter implements Converter, Serializable {
  
          @ManagedProperty(value = "#{cController}")
          private CustomerController cc;
  
          @Override
          public Object getAsObject(FacesContext context, UIComponent component,
                  String value) {
  
              Long pk = Long.parseLong(value);
              Customer c = null;
              List&lt;Customer&gt; list = (List&lt;Customer&gt;) cc.getCustomerList();
              for (Customer k : list) {
                  if (k.getCustomerId().equals(pk))
                          c = k;
              }
  
              return c;
          }
  
          @Override
          public String getAsString(FacesContext context, UIComponent component,
                  Object value) {
              Long id = (Long) value;
              return id.toString();
          }
  
          public CustomerController getCc() {
              return cc;
          }
  
          public void setCc(CustomerController cc) {
              this.cc = cc;
          }
      }
  </code></pre>
  
  <p>And finally the .xhtml file</p>
  
  <pre><code>&lt;h:head&gt;
      &lt;title&gt;Save Customer&lt;/title&gt;
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
      &lt;h:panelGrid columns="1"&gt;
  
          &lt;h:form&gt;
              &lt;h:messages id="messages"&gt;&lt;/h:messages&gt;
              &lt;p/&gt;
              &lt;h:selectOneMenu id="select1" value="#{customerController.customer}" converter="CustomerConverter"&gt;
  
                  &lt;f:selectItems 
                      value="#{customerController.customerList}" var="c"
                      itemLabel="#{c.customerId} #{c.firstName} #{c.lastName}"
                      itemValue="#{c.customerId}"/&gt;
              &lt;/h:selectOneMenu&gt;
              &lt;p/&gt;
              &lt;h:commandButton value="Output" update="output2"&gt;&lt;/h:commandButton&gt;
  
              &lt;p/&gt;
              &lt;h:outputText id="output2" value="#{customerController.customerDetails}"/&gt;
          &lt;/h:form&gt;
  
      &lt;/h:panelGrid&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Basically I seem to have trouble instantiating the CustomerController bean withing the CustomerConverter bean. Any ideas what I'm doing wrong?</p>