completeMethod primeFaces is returned the id where it should return name

Asked

Viewed 92 times

0

I am creating a form where I have neighborhood address name etc, and for that, I am using prime Faces.

<p:outputLabel value="Nome" for="nome" />        
<p:autoComplete id="nome" size="60"
            value="#{cadastroFuncionarioBean.funcionario.pessoa}"
            completeMethod="#{cadastroFuncionarioBean.pesquisarNomes}" 
            itemLabel="#{pessoa.nome}" />

When I create the auto complements it works perfectly, but when I return a search it returns the person ID instead of the name.

I think the problem is in my Personal Converter.java, which as can be seen below is returning the id.

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

import br.com.model.Pessoa;
import br.com.repository.Pessoas;

@FacesConverter(forClass = Pessoa.class)
public class PessoaConverter implements Converter {
    @Inject // funciona graças ao OmniFaces
    private Pessoas pessoas;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Pessoa retorno = null;
        if (value != null) {
            retorno = this.pessoas.porId(new Long(value));
        }
        return retorno;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value != null) {
        return ((Pessoa) value).getId().toString();
        }
        return null;
    }


}

well so, comes the question need to really create a class converterName,converterEndereco and converterBairro etc.. for each attribute of my person class as below.

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "pessoa")
public class Pessoa implements Serializable {
    private static final long serialVersionUID = 1L;
    Long id;

    private String nome;
    private String endereco;
    private String numero;
    private String complemento;
    private String bairro;
    private String cidade;
    private String estado;
    private String cep;


    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NotEmpty
    @Size(max = 100)
    @Column(length = 100, nullable = false)
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @NotEmpty
    @Size(max = 100)
    @Column(length = 100, nullable = false)
    public String getEndereco() {
        return endereco;
    }

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

    @NotEmpty
    @Size(max = 6)
    @Column(length = 6, nullable = false)
    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }


    //@NotEmpty
    @Size(max = 50)
    @Column(length = 50, nullable = false)
    public String getComplemento() {
        return complemento;
    }

    public void setComplemento(String complemento) {
        this.complemento = complemento;
    }

    @NotEmpty
    @Size(max = 30)
    @Column(length = 30, nullable = false)
    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }

    @NotEmpty
    @Size(max = 30)
    @Column(length = 30, nullable = false)
    public String getCidade() {
        return cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }

    @NotEmpty
    @Size(max = 2)
    @Column(length = 2, nullable = false)
    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    @NotEmpty
    @Size(max = 9)
    @Column(length = 9, nullable = false)
    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.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;
        Pessoa other = (Pessoa) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}

Or is there a more elegant way to do it?

  • Cara looked at the page and has the itemValue tag , already tried to use it?

  • Good morning! Macario1983, yes as you saw on the page but I don’t think I know how to make the correct use of the itemValue tag. If you can give me a hint or point out the error I’m very grateful I’ve been at it for days.

  • Dude, I think you can put the.id object in the itemvalue, in the case of the convert you even debug?

  • yes but my convert returns the Id

  • Which object is returning in the convert? A String?

  • yes @Override public String getAsString(Facescontext context, Uicomponent Component, Object value) { if (value != null) { Return ((Person) value). getId(). toString(); } Return null; }

  • Dude I think this method is responsible for rendering on the page what you want, and the other method is the return of the screen to the object, tries to use the itemvalue, without converting

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.