Problems searching for a data by ID in jpa

0

Good afternoon everyone, I am working on a practice project to increase my skills in JAVA EE, the fact is that I am doing a billing system, using JSF, JPA and PrimeFaces, I have a billing page and within this I have a page that takes the client code of a client and using ajax I execute a method to find the information of this. this is a portion of the invoice code.xhtml

<h:panelGrid id="gridBuscarDatosCliente" columns="4">
    <p:outputLabel value="Codigo del cliente" style="font-weight: bold"/>

    <p:inputText value="#{facturaBean.cliente.codigoCliente}" size="12">
        <p:ajax event="keyup" listener="#{facturaBean.agregarDatosCliente()}"
                update=":formularioFactura:gridDatosCliente"/>
    </p:inputText>

    <p:commandButton value="..." oncomplete="PF('dialogoClientes').show();"
                     update=":formularioFactura:dlgClientes"/>

    <p:outputLabel value="Fecha" style="font-weight: bold"/>
</h:panelGrid>

This is the section that I intend to update every time the client's code is typed in the input:

<p:separator/>

<h:panelGrid id="gridDatosCliente">
    <p:outputLabel value="Nombre: " style="font-weight: bold"/>
    <p:outputLabel id="lblnombreCliente" value="#{facturaBean.cliente.nombre} #{facturaBean.cliente.apellido}" 
                   style="font-weight: bold"/>

    <p:outputLabel value="Direccion:   N/A" style="font-weight: bold"/>
    <p:outputLabel/>
</h:panelGrid>

and this is myBean bill

    /*
 * 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 com.bean;

import com.pojo.Cliente;
import com.pojo.Factura;
import com.servicios.ClienteFacadeLocal;
import com.servicios.FacturaFacadeLocal;
import com.servicios.VendedorFacadeLocal;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Ana Sofia
 */
@Named(value = "facturaBean")
@RequestScoped
public class FacturaBean {


    @EJB
    private ClienteFacadeLocal clienteFacade;
    @EJB
    private FacturaFacadeLocal facturaFacade;


    //Este cliente es la llave foranea
    private Cliente cliente;
    private Factura factura;

    public FacturaBean() {
        cliente = new Cliente();
    }

    public ClienteFacadeLocal getClienteFacade() {
        return clienteFacade;
    }

    public void setClienteFacade(ClienteFacadeLocal clienteFacade) {
        this.clienteFacade = clienteFacade;
    }

    public Cliente getCliente() {
        return cliente;
    }

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

    public Factura getFactura() {
        return factura;
    }

    public void setFactura(Factura factura) {
        this.factura = factura;
    }

    public void agregarDatosCliente(){

        factura.setCodigoCliente(clienteFacade.find(cliente.getCodigoCliente()));
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Correcto", "Datos del cliente agregado"));
    }
}

What do you recommend me to do? Thanks in advance.

    
asked by Hildemy Borely 17.02.2017 в 22:07
source

1 answer

0

As I see it, what you are trying to do is filter the Client's code (as you are typing) to show it later on the invoice (as long as you find results). In this case it would be:

invoice.xhtml

<h:form>
  <p:inputText value="#{facturaBean.codigoClienteFiltro}" size="12">
    <p:ajax event="keyup" listener="#{facturaBean.agregarDatosCliente()}" update="@([id$=gridDatosCliente])"/>
  </p:inputText>


  <h:panelGrid id="gridDatosCliente">
    <p:outputLabel value="Nombre: " style="font-weight: bold"/>
    <p:outputLabel id="lblnombreCliente" value="#{facturaBean.factura.cliente.nombre} {facturaBean.factura.cliente.apellido}" style="font-weight: bold"/>
  </h:panelGrid>
</h:form>

facturaBean

@ViewScoped is the type of bean indicated for this case, since it will maintain the information of the data while the user interacts in the view.

I suggest declaring a variable of type according to the database (for this example it will be INT) to perform the filter and do not forget to implement Serializable:

@ViewScoped 
public class FacturaBean implements Serializable{

  @EJB
  private ClienteFacadeLocal clienteFacade;

  private int codigoClienteFiltro;
  private Factura factura;

  // Setters y getters

  public void agregarDatosCliente(){
     try {
        if (!String.valueOf(codigoClienteFiltro).isEmpty()) { //validamos que no esté vacío
            factura = new Factura(); //instanciamos la clase Factura
            Cliente cliente = clienteFacade.find(codigoClienteFiltro); //Buscamos el cliente
            if (cliente != null) { //Si encontró el cliente ->
                factura.setCliente(cliente); //Asignamos el cliente a la factura
             }
         }
     } catch (Exception e) {
         System.out.println("Error al filtrar: " + e.getMessage());
     }
  }

}

I hope it has been helpful. I remain attentive to any concerns; -)

    
answered by 21.02.2017 / 15:11
source