'null converter'的转换错误设置值

时间:2014-03-23 17:19:01

标签: jsf primefaces converter selectonemenu

我收到此转换错误消息。我正试图从selectOneMenu列表中获得一个客户,然后为他添加奖金。 我实现了转换器和equals / hashcode方法。 你能看到什么问题吗?

我的XHTML:

        <h:body>
        <h:form>
            <p:panel id="panel" header="Cadastro de bônus">
                <p:panelGrid columns="2">
                    <p:outputLabel value="Cliente: " />
                    <p:selectOneMenu value="#{clienteBean.cliente}">
                    <f:selectItem itemLabel="--Selecione um cliente--" />
                        <f:selectItems value="#{clienteBean.selectClientes}" />
                    </p:selectOneMenu>

                    <p:outputLabel value="Informe o valor da venda:" for="valor_venda" />
                    <p:inputText id="valor_venda"
                        value="#{bonusBean.bonus.valor_venda}" />

                    <h:commandButton value="Próximo" action="#{bonusBean.gravar}" />
                </p:panelGrid>
            </p:panel>
        </h:form>
    </h:body>
</ui:define>

我的道:

    public class DAOCliente {

public EntityManager getEntityManager() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("cliente");
    EntityManager entityManager = factory.createEntityManager();
    return entityManager;
}

public void adiciona (Cliente cliente){

    EntityManager entityManager = getEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(cliente);
    entityManager.getTransaction().commit();
    entityManager.close();

}

public void excluir(Cliente cliente)throws Exception{
    EntityManager entityManager = getEntityManager();
    try {
        entityManager.getTransaction().begin();
        cliente = entityManager.merge(cliente);
        entityManager.remove(cliente);
        entityManager.getTransaction().commit();
    } finally {
        entityManager.close();
    }
}

public Cliente consultar (Long id){
    Cliente cliente = new Cliente();
    EntityManager entityManager = getEntityManager();
    try {
        entityManager.getTransaction().begin();
        cliente = entityManager.find(Cliente.class, id);
        entityManager.getTransaction().commit();    
    }
    catch (Exception e) {
        entityManager.getTransaction().rollback();
    }
    finally {
        entityManager.close();
    }
    return cliente;
}

@SuppressWarnings("unchecked")
public List<Cliente> listarTodosClientes() throws Exception{
    EntityManager entityManager = getEntityManager();
    List<Cliente> lista = null;
    try {
        Query query = entityManager.createQuery("SELECT c FROM Cliente c");
        lista = query.getResultList();
        for (Cliente cliente : lista) {
            System.out.println("Nome: " + cliente.getNome());
            System.out.println("Fone Residencia: " + cliente.getFone_residencia());
            System.out.println("Fone Celular: " + cliente.getFone_celular());
            System.out.println("Perfil FB: " + cliente.getPerfil_facebook());
            System.out.println("------------------------------------");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        entityManager.close();
    }
    return lista;
}

}

我的转换器:

@FacesConverter(forClass = ClienteBean.class)
public class ConverterCliente implements Converter {

@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {

    if(value!=null && value.trim().length()>0){
        Long id = Long.valueOf(value);
        DAOCliente dao = new DAOCliente();
        return dao.consultar(id);
    }
    return null;
}

@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object value) {

    if(value!=null){
        Cliente cliente = (Cliente) value;
        return cliente.getId().toString();
    }
    return null;
}

}

My Managed Bean:

    @ManagedBean
    public class ClienteBean {

private Cliente cliente;
List<Cliente> clientes;
List<SelectItem> selectClientes;
DAOCliente dao = new DAOCliente();

public List<SelectItem> getSelectClientes() throws Exception {
    if(selectClientes==null){
        selectClientes = new ArrayList<SelectItem>();
        clientes = dao.listarTodosClientes();
        if(clientes!=null && !clientes.isEmpty()){
            SelectItem item;
            for(Cliente lista : clientes){
                item = new SelectItem(lista, lista.getNome());
                selectClientes.add(item);
            }
        }
    }
    return selectClientes;
}

public List<Cliente> getClientes() throws Exception{

    if(clientes == null){
        clientes = dao.listarTodosClientes();
    }
    return clientes;
}

public Cliente getCliente() {
    return cliente;
}

public void gravar() {
    dao.adiciona(cliente);
    this.cliente = new Cliente();
}

public void excluir(Cliente cliente) throws Exception{
    dao.excluir(cliente);
}

public void setClientes(List<Cliente> clientes) {
    this.clientes = clientes;
}

}

My Cliente课程:

    public class Cliente {

@Id @GeneratedValue
private Long id;

private String nome;
private String fone_residencia;
private String fone_celular;
private String perfil_facebook;

@OneToMany(mappedBy = "cliente", fetch = FetchType.EAGER, targetEntity = Bonus.class, cascade = CascadeType.ALL)
private List<Bonus> bonus;

public List<Bonus> getBonus() {
    return bonus;
}
public void setBonus(List<Bonus> bonus) {
    this.bonus = bonus;
}

public String getPerfil_facebook() {
    return perfil_facebook;
}
public void setPerfil_facebook(String perfil_facebook) {
    this.perfil_facebook = perfil_facebook;
}
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getFone_residencia() {
    return fone_residencia;
}
public void setFone_residencia(String fone_residencia) {
    this.fone_residencia = fone_residencia;
}
public String getFone_celular() {
    return fone_celular;
}
public void setFone_celular(String fone_celular) {
    this.fone_celular = fone_celular;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((bonus == null) ? 0 : bonus.hashCode());
    result = prime * result
            + ((fone_celular == null) ? 0 : fone_celular.hashCode());
    result = prime * result
            + ((fone_residencia == null) ? 0 : fone_residencia.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((nome == null) ? 0 : nome.hashCode());
    result = prime * result
            + ((perfil_facebook == null) ? 0 : perfil_facebook.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Cliente other = (Cliente) obj;
    if (bonus == null) {
        if (other.bonus != null)
            return false;
    } else if (!bonus.equals(other.bonus))
        return false;
    if (fone_celular == null) {
        if (other.fone_celular != null)
            return false;
    } else if (!fone_celular.equals(other.fone_celular))
        return false;
    if (fone_residencia == null) {
        if (other.fone_residencia != null)
            return false;
    } else if (!fone_residencia.equals(other.fone_residencia))
        return false;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if (nome == null) {
        if (other.nome != null)
            return false;
    } else if (!nome.equals(other.nome))
        return false;
    if (perfil_facebook == null) {
        if (other.perfil_facebook != null)
            return false;
    } else if (!perfil_facebook.equals(other.perfil_facebook))
        return false;
    return true;
}
@Override
public String toString() {
    return "Cliente [id=" + id + ", nome=" + nome + ", fone_residencia="
            + fone_residencia + ", fone_celular=" + fone_celular
            + ", perfil_facebook=" + perfil_facebook + ", bonus=" + bonus
            + "]";
}

}

1 个答案:

答案 0 :(得分:0)

您应该在相关标签中提及使用的转换器类以启用转换:

<p:selectOneMenu value="#{clienteBean.cliente}" converter="converterCliente">
    ...                
</p:selectOneMenu>