1

I am trying to make two selectOneMenu nested, one contains the provinces and the second the cities of those provinces, but I haves a mistake, when I select the province, and I can not find out what I did wrong !. If anyone can give me a hand with deciphering the error, already very grateful!

The error itself:

SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default 
task-22) javax.faces.component.UpdateModelException: 
java.lang.IllegalArgumentException:
Cannot convert 2 of type class java.lang.Integer to class 
ar.com.kompass.model.Provincia
at javax.faces.component.UIInput.updateModel(UIInput.java:866)
at javax.faces.component.UIInput.processUpdates(UIInput.java:749)
at com.sun.faces.context.PartialViewContextImpl$Phase
AwareVisitCallback.visit(PartialViewContextImpl.java:577)
at com.sun.faces.component.visit.PartialVisitContext.invoke
VisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1689)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)

If I do not understand, the message "Can not convert 2 of ...." refers to the value selected, in this case the value of 2 provinces, and that does not manage to "convert" to a province object? .... how to solve this? The piece of view code that generates the error:

<p:row>
                <p:column>
                    <p:outputLabel value="Provincia " />
                    <p:selectOneMenu id="cboProvincia"
                        value="#{cuentaBean.cuenta.provincia}" required="true"
                        requiredMessage="Debe seleccionar una provincia"
                        converter="omnifaces.SelectItemsConverter">
                        <f:selectItem itemLabel="--Seleccione--" itemValue="#{null}"
                            noSelectionOption="true" />
                        <f:selectItems value="#{cuentaBean.lstProvincias}" var="prov"
                            itemLabel="#{prov.nombre}" itemValue="#{prov.id}" />
                        <f:ajax event="change"
                            listener="#{cuentaBean.listarLocalidades()}"
                            execute="cboProvincia" render="cboLocalidad" />
                    </p:selectOneMenu>
                </p:column>

                <p:column>
                    <p:outputLabel value="Localidad " />
                    <p:selectOneMenu id="cboLocalidad"
                        value="#{cuentaBean.cuenta.localidad}" required="true"
                        requiredMessage="Debe seleccionar una Localidad"
                        converter="omnifaces.SelectItemsConverter">
                        <f:selectItem itemLabel="--Seleccione--" itemValue="#{null}"
                            noSelectionOption="true" />
                        <f:selectItems value="#{cuentaBean.lstLocalidades}" var="loca"
                            itemLabel="#{loca.nombre}" itemValue="#{loca}" />
                    </p:selectOneMenu>
                </p:column>
            </p:row>

and this is the bean:

@Named
@ViewScoped public class CuentaBean implements Serializable {

@Inject
private ICuentaService cuentaService;

@Inject
private Cuenta cuenta;
@Inject
private IProvinciaService provinciaService;
@Inject
private ILocalidadService localidadService;

private List<Cuenta> lstCuentas;
private List<Provincia> lstProvincias;
private List<Localidad> lstLocalidades;
private int codigoProvincia;

public int getCodigoProvincia() {
    return codigoProvincia;
}

public void setCodigoProvincia(int codigoProvincia) {
    this.codigoProvincia = codigoProvincia;
}

public Cuenta getCuenta() {
    return cuenta;
}

public void setCuenta(Cuenta cuenta) {
    this.cuenta = cuenta;
}

@PostConstruct
public void init(){     
   lstCuentas = new ArrayList<>();
   lstProvincias = new ArrayList<>();
   lstLocalidades = new ArrayList<>();
   this.listarProvincias();
}

public List<Cuenta> getLstCuentas() {
   return lstCuentas;
}

public void setLstCuentas(List<Cuenta> lstCuentas) {
   this.lstCuentas = lstCuentas;
}

public List<Provincia> getLstProvincias() {
   return lstProvincias;
}

public void setLstProvincias(List<Provincia> lstProvincias) {
   this.lstProvincias = lstProvincias;
}

public List<Localidad> getLstLocalidades() {
   return lstLocalidades;
}

public void setLstLocalidades(List<Localidad> lstLocalidades) {
   this.lstLocalidades = lstLocalidades;
}

public void listarProvincias() {
    try {
      //lstCuentas = cuentaService.listar();
      lstProvincias= provinciaService.listar();
      //lstLocalidades= localidadService.listar(idProv);      
    } catch (Exception e) {

    }
 } 

 public void listarLocalidades
    System.out.print(this.codigoProvincia);
    lstLocalidades= localidadService.listar(this.codigoProvincia);          
} catch (Exception e) {

  }
 }
}

And finally the Model Cuenta, that perhaps could be the reason for the error:

@Entity @Table(name = "cuenta") public class Cuenta  implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "nombre", length = 30, nullable = false)
private String nombre;
@Column(name = "domicilio", length = 30, nullable = false)
private String domicilio;
private short altura;

@OneToOne
@JoinColumn(name="idprov" , nullable = false)
private Provincia provincia;

@OneToOne
@JoinColumn(name="idloca" , nullable = false)
private Localidad localidad;

public int getId() {
   return id;
}
public void setId(int id) {
    this.id = id;
}
public String getNombre() {
   return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public String getDomicilio() {
    return domicilio;
}
public void setDomicilio(String domicilio) {
    this.domicilio = domicilio;
}
public short getAltura() {
    return altura;
}
public void setAltura(short altura) {
   this.altura = altura;
}

public Localidad getLocalidad() {
   return localidad;
}
public void setLocalidad(Localidad localidad) {
   this.localidad = localidad;
}

public Provincia getProvincia() {
   return provincia;
}
public void setProvincia(Provincia provincia) {
   this.provincia = provincia;
}

@Override
public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + id;
   return result;
}
@Override
public boolean equals(Object obj) {
   if (this == obj)
       return true;
   if (obj == null)
       return false;
   if (getClass() != obj.getClass())
       return false;
   Cuenta other = (Cuenta) obj;
   if (id != other.id)
       return false;
   return true;
 }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Good exceptions are excellent [search keywords](https://www.google.com/search?q=javax.faces.component.UpdateModelException%3A+java.lang.IllegalArgumentException%3A+Cannot+convert+of+type+class+java.lang.Integer+to+class&oq=javax.faces.component.UpdateModelException%3A+java.lang.IllegalArgumentException%3A+Cannot+convert+of+type+class+java.lang.Integer+to+class) – BalusC Sep 29 '17 at 13:18

1 Answers1

2

You have it wrong:

 <f:selectItems value="#{cuentaBean.lstProvincias}" var="prov"
                        itemLabel="#{prov.nombre}" itemValue="#{prov.id}" />

It should be:

 <f:selectItems value="#{cuentaBean.lstProvincias}" var="prov"
                        itemLabel="#{prov.nombre}" itemValue="#{prov}" />

Explanation: You are trying to set the id of provincia which is int to provincia which is object.

Milkmaid
  • 1,659
  • 4
  • 26
  • 39