Error converting object to string jsf-hibernate

0

I am doing a control program for borrowed books where only the Isbn of the book and the Student's card are kept in the Loan table; I do it with JSF and Hibernate. I'm loading a selectOneMenu with a query of Student-type objects but when converting it to an object (using converter) it gives me the following error:

javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert 1-null of type class clases.Estudiante to class java.lang.Integer

The card field is defined as Integer and name as String. When the combo is loaded with the Student objects, I only show the two fields that interest me: the name and the card, which are the ones I try to convert into the conveter. The combo is normally loaded without problems, the problem arises when I want to save the data because it does not convert the string to the Student object, that is, it does not allow me to separate the String in the fields name and carnet to manipulate them as Student objects.

Here the SelectOneMenu

<h:selectOneMenu id="cbIsbn" converter="libroConverter" 
value="#{prestamoBean.elPrestamo.id.isbn}">
<f:selectItems value="#{prestamoBean.llenaLibro()}"/>
 </h:selectOneMenu>
 <h:message for="cbIsbn" style="color:red;"/>

Here the converter

@FacesConverter("estudianteConverter")
public class EstudianteConverter implements Converter {
    /** vea @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)*/
    public Object getAsObject(FacesContext context, UIComponent arg1, String valor) {
            String[] valores = valor.split("-");
            if(valor != null){
                            Estudiante est = new Estudiante();
                            Integer car = Integer.valueOf(valores[0]);
                            est.setCarnet(car);
                            // est.setCarnet(Integer.parseInt(valores[0]));
                // est.setNombre(valores[1].trim());
                return est;             
            } else{     
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Error 1 de conversion de Estudiante");
                throw new ConverterException(msg);      
            }
    }
    /** vea @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)*/
    public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
        String valor = "";
        if(object!=null){
        if(object instanceof Estudiante){           
        Estudiante est = (Estudiante) object;       
        // return est.getCarnet().toString();
                valor = est.getCarnet().toString()+"-"+est.getNombre(); //+ "~" +cl.getDireccion()+ "~" +cl.getMontoPagado()+ "~" +cl.getTelefono();        
        }else{          
        FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Error de conversion, el objeto no corresponde a un estudiante");
                throw new ConverterException(msg);      
            }
        }else{
            valor = null;
        }
        return valor;
    }
} 

Here the class is its Integer-type fields and types

@Entity
@Table(name="estudiante", catalog="sipreli")
public class Estudiante  implements java.io.Serializable {
     private Integer carnet;
     private String nombre;
     private Integer telefono;
     private String direccion;
     private String email;
     private Integer escuela;
     private Set<Prestamo> prestamos = new HashSet<Prestamo>(0);
    public Estudiante() {
    }   
    public Estudiante(Integer carnet) {
        this.carnet = carnet;
    }
    public Estudiante(Integer carnet, String nombre, Integer telefono, String direccion, String email, Integer escuela, Set<Prestamo> prestamos) {
       this.carnet = carnet;
       this.nombre = nombre;
       this.telefono = telefono;
       this.direccion = direccion;
       this.email = email;
       this.escuela = escuela;
       this.prestamos = prestamos;
    }
    @Id 
    @Column(name="carnet", unique=true, nullable=false)
    public Integer getCarnet() {
        return this.carnet;
    }
    public void setCarnet(Integer carnet) {
        this.carnet = carnet;
    }    
    @Column(name="nombre", length=45)
    public String getNombre() {
        return this.nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }    
    @Column(name="telefono")
    public Integer getTelefono() {
        return this.telefono;
    }
    public void setTelefono(Integer telefono) {
        this.telefono = telefono;
    }
    @Column(name="direccion", length=45)
    public String getDireccion() {
        return this.direccion;
    }
    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }
    @Column(name="email", length=45)
    public String getEmail() {
        return this.email;
    }
    public void setEmail(String email) {
        this.email = email;
    }    
    @Column(name="escuela")
    public Integer getEscuela() {
        return this.escuela;
    }
        public void setEscuela(Integer escuela) {
        this.escuela = escuela;
    }
    @OneToMany(fetch=FetchType.LAZY, mappedBy="estudiante")
    public Set<Prestamo> getPrestamos() {
        return this.prestamos;
    }  
    public void setPrestamos(Set<Prestamo> prestamos) {
        this.prestamos = prestamos;
    }
    @Override
    public String toString() {
        return carnet + "-" + nombre; //To change body of generated methods, choose Tools | Templates.
    }
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.carnet);
        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 Estudiante other = (Estudiante) obj;
        if (!Objects.equals(this.carnet, other.carnet)) {
            return false;
        }
        return true;
    }
    
asked by user80385 24.03.2018 в 01:33
source

0 answers