title: Picklist converter
body: <p>I´m having a problem with my picklist I thing it´s because I don´t have a converter, when I try to view the items that I choose clicking in the commandbutton, I get the following exception:</p>
  
  <pre><code>java.lang.ClassCastException: java.lang.String cannot be cast to model.Employee
  </code></pre>
  
  <p>Here´s the code:</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://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"&gt;
  &lt;h:head&gt;
  &lt;title&gt;PickList Test&lt;/title&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;h:form id="form"&gt;
               &lt;p:pickList value="#{pickListBean.employeeList}" var="employee" itemLabel="#  {employee.employeeName}" converter="pickListConverter" itemValue="#{employee.employeeCode}" /&gt;  
               &lt;p:commandButton value="View targets" action="#{pickListBean.viewTargets}" style="margin-    left: 12px;"/&gt;
      &lt;/h:form&gt;
  
  
   &lt;/h:body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>bean</p>
  
  <pre><code>@ManagedBean
  @RequestScoped
  public class PickListBean {
  @EJB
  private BussinessList bl = new BussinessList();
  private DualListModel&lt;Employee&gt; employeeList;
  List&lt;Employee&gt; source = new ArrayList&lt;Employee&gt;();
  List&lt;Employee&gt; target = new ArrayList&lt;Employee&gt;();
  private Employee employee;
  /**
  * Creates a new instance of PickListBean
  */
  public PickListBean() {
  
  
  }
  @PostConstruct
  public void postConstruct(){
  source = bl.getEmployeeList();
  employeeList = new DualListModel&lt;Employee&gt;(source, target);
  }
  public void viewTargets(){
      target = employeeList.getTarget();
      for(int i = 0; i &lt; target.size(); i++)
          System.out.println("Targets: " + target.get(i).getEmployeeName());
  }
  public DualListModel&lt;Employee&gt; getEmployeeList() {
  return employeeList;
  }
  
  public void setEmployeeList(DualListModel&lt;Employee&gt; employeeList) {
  this.employeeList = employeeList;
  }
   public Employee getEmployee() {
   return employee;
   }
  
   public void setEmployee(Employee employee) {
  this.employee= employee;
  }
  }
  </code></pre>
  
  <p>converter</p>
  
  <pre><code>@FacesConverter(value = "pickListConverter")
  public class PickListConverter implements Converter {
  private static Map&lt;Object, String&gt; entities = new WeakHashMap&lt;Object, String&gt;();
  
  @Override
  public String getAsString(FacesContext context, UIComponent component, Object entity) {
      synchronized (entities) {
          if (!entities.containsKey(entity)) {
              String uuid = UUID.randomUUID().toString();
              entities.put(entity, uuid);
              return uuid;
          } else {
              return entities.get(entity);
          }
      }
  }
  
  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
      for (Entry&lt;Object, String&gt; entry : entities.entrySet()) {
          if (entry.getValue().equals(uuid)) {
              return entry.getKey();
          }
      }
      return null;
  }
  
  }
  </code></pre>
  
  <p>Employee = Funcionario</p>
  
  <pre><code>  @Entity
    @Table(name = "funcionario")
   @XmlRootElement
   @NamedQueries({
  @NamedQuery(name = "Funcionario.findAll", query = "SELECT f FROM Funcionario f"),
  @NamedQuery(name = "Funcionario.findByCodigoFuncionario", query = "SELECT f FROM   Funcionario f WHERE f.codigoFuncionario = :codigoFuncionario"),
  @NamedQuery(name = "Funcionario.findByNomeFuncionario", query = "SELECT f FROM Funcionario f WHERE f.nomeFuncionario = :nomeFuncionario"),
  @NamedQuery(name = "Funcionario.findByUsernameFuncionario", query = "SELECT f FROM    Funcionario f WHERE f.usernameFuncionario = :usernameFuncionario"),
  @NamedQuery(name = "Funcionario.findBySenhaFuncionario", query = "SELECT f FROM    Funcionario f WHERE f.senhaFuncionario = :senhaFuncionario")})
  public class Funcionario implements Serializable{
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "codigo_funcionario")
  private Integer codigoFuncionario;
  @Size(max = 45)
  @Column(name = "nome_funcionario")
  private String nomeFuncionario;
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 300)
  @Column(name = "username_funcionario")
  private String usernameFuncionario;
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 300)
  @Column(name = "senha_funcionario")
  private String senhaFuncionario;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "codigoFuncionario")
  private Collection&lt;EquipaResponsavel&gt; equipaResponsavelCollection;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "funcionario")
  private Collection&lt;EquipaFuncionario&gt; equipaFuncionarioCollection;
  @OneToMany(mappedBy = "codigoFuncionario")
  private Collection&lt;Ticket&gt; ticketCollection;
  
  public Funcionario() {
  }
  
  public Funcionario(Integer codigoFuncionario) {
      this.codigoFuncionario = codigoFuncionario;
  }
  
  public Funcionario(Integer codigoFuncionario, String usernameFuncionario, String senhaFuncionario) {
      this.codigoFuncionario = codigoFuncionario;
      this.usernameFuncionario = usernameFuncionario;
      this.senhaFuncionario = senhaFuncionario;
  }
  
  public Integer getCodigoFuncionario() {
      return codigoFuncionario;
  }
  
  public void setCodigoFuncionario(Integer codigoFuncionario) {
      this.codigoFuncionario = codigoFuncionario;
  }
  
  public String getNomeFuncionario() {
      return nomeFuncionario;
  }
  
  public void setNomeFuncionario(String nomeFuncionario) {
      this.nomeFuncionario = nomeFuncionario;
  }
  
  public String getUsernameFuncionario() {
      return usernameFuncionario;
  }
  
  public void setUsernameFuncionario(String usernameFuncionario) {
      this.usernameFuncionario = usernameFuncionario;
  }
  
  public String getSenhaFuncionario() {
      return senhaFuncionario;
  }
  
  public void setSenhaFuncionario(String senhaFuncionario) {
      this.senhaFuncionario = senhaFuncionario;
  }
  
  @XmlTransient
  public Collection&lt;EquipaResponsavel&gt; getEquipaResponsavelCollection() {
      return equipaResponsavelCollection;
  }
  
  public void setEquipaResponsavelCollection(Collection&lt;EquipaResponsavel&gt; equipaResponsavelCollection) {
      this.equipaResponsavelCollection = equipaResponsavelCollection;
  }
  
  @XmlTransient
  public Collection&lt;EquipaFuncionario&gt; getEquipaFuncionarioCollection() {
      return equipaFuncionarioCollection;
  }
  
  public void setEquipaFuncionarioCollection(Collection&lt;EquipaFuncionario&gt; equipaFuncionarioCollection) {
      this.equipaFuncionarioCollection = equipaFuncionarioCollection;
  }
  
  @XmlTransient
  public Collection&lt;Ticket&gt; getTicketCollection() {
      return ticketCollection;
  }
  
  public void setTicketCollection(Collection&lt;Ticket&gt; ticketCollection) {
      this.ticketCollection = ticketCollection;
  }
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (codigoFuncionario != null ? codigoFuncionario.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Funcionario)) {
          return false;
      }
      Funcionario other = (Funcionario) object;
      if ((this.codigoFuncionario == null &amp;&amp; other.codigoFuncionario != null) || (this.codigoFuncionario != null &amp;&amp; !this.codigoFuncionario.equals(other.codigoFuncionario))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "modelo.Funcionario[ codigoFuncionario=" + codigoFuncionario + " ]";
  }
  
  }
  </code></pre>