Problem with JSF during form validation

0

I have a problem with the animal registration form:
Here's the part of the diagram involved in the form relationship:


TheproblemisthatwhenIamregisteringananimal,Icangetbythename,age,genderandtypeofanimal(whichinthiscaseisfromanothertable).InthefieldCustomerIcanfillinthedata,butatthetimeofclickingsave,itappearsonthescreenthatthecustomer'sfieldisnotfilledinandthefieldfilledin.

Followtheprojectcodes:

  

ClassPerson:

packagedominio.modelo;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.Id;importjavax.persistence.Inheritance;importjavax.persistence.InheritanceType;@Entity@Inheritance(strategy=InheritanceType.JOINED)publicclassPessoa{@Id@Column(length=14)privateStringcpf;@Column(length=25,nullable=false)protectedStringnome;@Column(length=25,nullable=false)privateStringsobrenome;@Column(length=9,nullable=true)privateStringtelefone;publicPessoa(){}publicPessoa(Stringcpf,Stringnome,Stringsobrenome,Stringtelefone){this.cpf=cpf;this.nome=nome;this.sobrenome=sobrenome;this.telefone=telefone;}publicStringgetCpf(){returncpf;}publicvoidsetCpf(Stringcpf){this.cpf=cpf;}publicStringgetNome(){returnnome;}publicvoidsetNome(Stringnome){this.nome=nome;}publicStringgetSobrenome(){returnsobrenome;}publicvoidsetSobrenome(Stringsobrenome){this.sobrenome=sobrenome;}publicStringgetTelefone(){returntelefone;}publicvoidsetTelefone(Stringtelefone){this.telefone=telefone;}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((cpf==null)?0:cpf.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!=obj.getClass())returnfalse;Pessoaother=(Pessoa)obj;if(cpf==null){if(other.cpf!=null)returnfalse;}elseif(!cpf.equals(other.cpf))returnfalse;returntrue;}}
  

ClassCustomer

packagedominio.modelo;importjava.util.List;importjavax.persistence.CascadeType;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.Inheritance;importjavax.persistence.InheritanceType;importjavax.persistence.OneToMany;@EntitypublicclassClienteextendsPessoa{@Column(length=20,nullable=true)privateintcredito;@OneToMany(mappedBy="cliente", cascade=CascadeType.PERSIST)
    private List<Animal> animais;

    public Cliente() {
        super();
    }

    public Cliente(String cpf, String nome, String sobrenome, String telefone) {
        super(cpf, nome, sobrenome, telefone);
    }

    public int getcredito() {
        return credito;
    }

    public void setCredito(int credito) {
        this.credito = credito;
    }

    public List<Animal> getAnimais() {
        return animais;
    }

    public void setAnimais(List<Animal> animais) {
        this.animais = animais;
    }

    @Override
    public String toString() {
        return nome;
    }

}
  

Animal Class

package dominio.modelo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;

@Entity
public class Animal {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_ANIMAL")
    @SequenceGenerator(name="ID_ANIMAL", sequenceName="SEQ_ID_ANIMAL", initialValue=1 ,allocationSize=1)
    private Integer id;

    @Column(length=25, nullable=false)
    private String nome;

    private Integer idade;

    @Column(nullable=false)
    private String sexo;

    @ManyToOne
    @JoinColumn(name="id_cliente")
    private Cliente cliente;

    @ManyToOne
    @JoinColumn(name="tipo")
    private Tipo tipo;

    public Animal() {
        super();
    }

    public Animal(Integer id, String nome, Integer idade, String sexo) {
        this.id = id;
        this.nome = nome;
        this.idade = idade;
        this.sexo = sexo;
    }

    public Integer getId() {
        return id;
    }

    public String getIdStr() {
        if (this.id == null)
            return "";

        return id.toString();
    }

    public String getNome() {
        return nome;
    }

    public String getNomeStr() {
        if (this.nome == null)
            return "";

        return nome.toString();
    }

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

    public Integer getIdade() {
        return idade;
    }

    public void setIdade(Integer idade) {
        this.idade = idade;
    }

    public String getSexo() {
        if (this.sexo == null)
            return "";

        return sexo.toString();
    }

    public String getSexoStr() {
        if (this.sexo == null)
            return "";

        return sexo.toString();
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Tipo getTipo() {
        return tipo;
    }

    public void setTipo(Tipo tipo) {
        this.tipo = tipo;
    }

}
  

Class Type

package dominio.modelo;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
public class Tipo {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_TIPO")
    @SequenceGenerator(name="ID_TIPO", sequenceName="SEQ_ID_TIPO", initialValue=1 ,allocationSize=1)
    private Integer codigo;

    @Column(unique=true)
    private String nome;

    @OneToMany(mappedBy="tipo")
    private List<Animal> animais;

    public Tipo() {
        super();
    }

    public Tipo(String nome) {
        this.nome = nome;
    }


    public Integer getCodigo() {
        return codigo;
    }

    public String getNome() {
        return nome;
    }

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

    public List<Animal> getAnimais() {
        return animais;
    }

    public void setAnimais(List<Animal> animais) {
        this.animais = animais;
    }

    @Override
    public String toString() {
        return nome;
    }

}
  

Page editAnimal.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:p="http://primefaces.org/ui"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

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

    <ui:define name="">Edição de Animal</ui:define>

    <ui:define name="conteudo">
        <p:panelGrid columns="2">

            <ui:remove>
                <p:outputLabel for="id" value="ID:" />
                <p:inputText id="id" value="#{animalMB.animal.id}" required="true" />
            </ui:remove>

            <p:outputLabel for="nome" value="Nome:" />
            <p:inputText id="nome" value="#{animalMB.animal.nome}" required="true" />

            <p:outputLabel for="idade" value="Idade:" />
            <p:inputText id="idade" value="#{animalMB.animal.idade}" required="true" />

            <p:outputLabel for="sexo" value="Sexo:" />
            <p:selectOneMenu id="sexo" value="#{animalMB.animal.sexo}" required="true">
                <f:selectItem itemValue="" itemLabel="Selecione" />
                <f:selectItem itemValue="M" itemLabel="Macho" />
                <f:selectItem itemValue="F" itemLabel="Fêmea" />
            </p:selectOneMenu>

            <p:outputLabel for="cliente" value="Cliente:" />
            <p:selectOneMenu id="cliente" value="#{animalMB.animal.cliente}" converter="cliente-converter" required="true">
                <f:selectItem itemValue="" itemLabel="Selecione" />
                <f:selectItems value="#{animalMB.clientes}" var="cli" itemLabel="#{cli.nome}" />
            </p:selectOneMenu>

            <p:outputLabel for="tipo" value="Tipo:" />
            <p:selectOneMenu id="tipo" value="#{animalMB.animal.tipo}" converter="tipo-converter" required="true">
                <f:selectItems value="#{animalMB.tipos}" var="tipo" itemLabel="#{tipo.nome}"></f:selectItems>
            </p:selectOneMenu>

            <h:outputLabel value="" />

            <h:panelGroup>
                <p:commandLink ajax="false" value="Salvar" action="#{animalMB.acaoSalvar}" />
            -
            <p:commandLink ajax="false" immediate="true" value="Cancelar" action="#{animalMB.acaoListar}" />

            </h:panelGroup>

        </p:panelGrid>

    </ui:define>

</ui:composition>
</html>
    
asked by anonymous 03.11.2017 / 00:26

1 answer

0

The itemValue property in its f:selectItems is missing, it serves to set the client on the animal object of its managed bean animalMB. As you have declared a convert and it constructs the client object from a cpf then pass the property cpf on itemValue on f:selectItems .

<p:outputLabel for="cliente" value="Cliente:" />
<p:selectOneMenu id="cliente" value="#{animalMB.animal.cliente}" converter="cliente-converter" required="true">
    <f:selectItem itemValue="" itemLabel="Selecione" />
    <f:selectItems value="#{animalMB.clientes}" var="cli" itemValue="#{cli.cpf}" itemLabel="#{cli.nome}"/>
</p:selectOneMenu>
    
03.11.2017 / 01:57