java.lang.NumberFormatException: For input string: "code"

0

I am developing a project with JSF, primefaces, maven and JPA with the specification of hibernate and PostgreeSQL DBMS (I created the separate database script, then connected with hibernate) and when I tried to run the page listing all the clients, I'm getting the following exception:

  

java.lang.NumberFormatException: For input string: "code"

I do not understand why this error occurs since the "code" (client id) is of type Integer and the database is also INTEGER. In the test in the main method it works without problem.

My JSF page:

<ui:composition template="./../template.xhtml">

            <ui:define name="SYS_MARKET CONNECT">
                SYS_MARKET CONNECT
            </ui:define>

            <ui:define name="left">
                left
            </ui:define>

            <ui:define name="right">
                right
            </ui:define>

            <ui:define name="content">
                <h:form>

                    <p:dataTable var="cliente" value="#{controlerCliente.obterClientes()}" rows="3" rowKey="#{cliente.codigo}"
                                 selection="#{controlerCliente.selectClientes}" selectionMode="single" paginator="true">

                        <p:column headerText="CODIGO: ">
                            <h:outputText value="#{cliente.codigo}"/>
                        </p:column>
                        <p:column headerText="NOME: ">
                            <h:outputText value="#{cliente.nome}"/>
                        </p:column>
                        <p:column headerText="CPF: ">
                            <h:outputText value="#{cliente.cpf}"/>
                        </p:column>
                        <p:column headerText="DATA CADASTRO">
                            <h:outputText value="#{cliente.dataAbertura}"/>
                        </p:column>

                        <p:column headerText="E-MAIL: ">
                            <h:outputText value="#{cliente.email}"/>
                        </p:column>

                        <p:column headerText="TELEFONE: ">
                            <h:outputText value="#{cliente.telefone}"/>
                        </p:column>

                        <p:column headerText="RUA: ">
                            <h:outputText value="#{cliente.endereco.bairro}"/>
                        </p:column>

                        <p:column headerText="NÚMERO: ">
                            <h:outputText value="#{cliente.endereco.numero}"/>
                        </p:column>
                        <p:column headerText="CEP: ">
                            <h:outputText value="#{cliente.endereco.cep}"/>
                        </p:column>


                    </p:dataTable>
                </h:form>
            </ui:define>

        </ui:composition>

follows my client class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.entidade;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Daniel
 */
@Entity
public class Cliente implements Serializable {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    @Column(name = "Id")
    private Integer codigo;

    @Column(nullable = false)
    private String nome;

    @Temporal(TemporalType.TIMESTAMP)
    private Date dataAbertura;

    @Column()
    private String cpf;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private String telefone;

    @Embedded
    private Endereco endereco;

    @OneToMany(mappedBy = "cliente_id", fetch = FetchType.EAGER)
    private List<Venda> venda;

    public Cliente(Integer codigo, String nome, Date dataAbertura, String cpf, String email, String telefone, Endereco endereco) {

        this.codigo = codigo;
        this.nome = nome;
        this.dataAbertura = dataAbertura;
        this.email = email;
        this.telefone = telefone;
        this.cpf = cpf;
        this.endereco = endereco;
    }

    public Cliente() {

    }

    public Integer getCodigo() {
        return codigo;
    }

    public void setCodigo(Integer codigo) {
        this.codigo = codigo;
    }

    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the dataAbertura
     */
    public Date getDataAbertura() {
        return dataAbertura;
    }

    /**
     * @param dataAbertura the dataAbertura to set
     */
    public void setDataAbertura(Date dataAbertura) {
        this.dataAbertura = dataAbertura;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the telefone
     */
    public String getTelefone() {
        return telefone;
    }

    /**
     * @param telefone the telefone to set
     */
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    /**
     * @return the cpf
     */
    public String getCpf() {
        return cpf;
    }

    /**
     * @param cpf the cpf to set
     */
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        this.endereco = endereco;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 41 * hash + Objects.hashCode(this.codigo);
        hash = 41 * hash + Objects.hashCode(this.nome);
        hash = 41 * hash + Objects.hashCode(this.dataAbertura);
        hash = 41 * hash + Objects.hashCode(this.cpf);
        hash = 41 * hash + Objects.hashCode(this.email);
        hash = 41 * hash + Objects.hashCode(this.telefone);
        hash = 41 * hash + Objects.hashCode(this.endereco);
        hash = 41 * hash + Objects.hashCode(this.venda);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Cliente other = (Cliente) obj;
        if (!Objects.equals(this.nome, other.nome)) {
            return false;
        }
        if (!Objects.equals(this.cpf, other.cpf)) {
            return false;
        }
        if (!Objects.equals(this.email, other.email)) {
            return false;
        }
        if (!Objects.equals(this.telefone, other.telefone)) {
            return false;
        }
        if (!Objects.equals(this.codigo, other.codigo)) {
            return false;
        }
        if (!Objects.equals(this.dataAbertura, other.dataAbertura)) {
            return false;
        }
        if (!Objects.equals(this.endereco, other.endereco)) {
            return false;
        }
        if (!Objects.equals(this.venda, other.venda)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Cliente{" + "codigo=" + codigo + ", nome=" + nome + ", dataAbertura=" + dataAbertura + ", cpf=" + cpf + ", email=" + email + ", telefone=" + telefone + ", endereco=" + endereco + ", venda=" + venda + '}';
    }

}

controler (clientBean):

@ManagedBean
@SessionScoped
public class ControlerCliente implements Serializable{

    private Cliente cliente;
    private transient ClienteModel clienteModel;
    private Endereco endereco;
    private List<Cliente> selectClientes = null;

        public ControlerCliente() {

        this.endereco = new Endereco();
        this.clienteModel = new ClienteModel();
        this.cliente=new Cliente();
        this.selectClientes = new ArrayList<>();

    }





listar todos da implementação do DAO:





@Override
    public List<Cliente> recuperarTodos() {
         entityManager = HibernateUtil.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            Query consult = entityManager.createQuery("select c from Cliente c");
            clientes = consult.getResultList();
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            System.out.println("erro ao listar os clientes");
            e.printStackTrace();
            entityManager.getTransaction().rollback();
        }finally{
            entityManager.close();
        }
        return clientes;

    }
    
asked by anonymous 18.06.2018 / 02:33

1 answer

1

The error occurs because the rowKey attribute of the p:dataTable component is of type String and you are passing Integer(id) .

What you can do is create a method that returns this attribute as String, for example:

public String getCodigoString(){
    String.valueOf(this.codigo);
}

and use as rowKey this method:

<p:dataTable var="cliente" value="#{controlerCliente.obterClientes()}" rows="3" 
    rowKey="#{cliente.codigoString}" 
    selection="#{controlerCliente.selectClientes}" selectionMode="single" paginator="true">
    
18.06.2018 / 04:25