Question

I have a problem, well when I save my object to the DB it works fine, but when i want to retrieve it from the DB doesn't work, I'm using selectItemsConverter by Omnifaces

I have my Object "Modelo" which has two other objects inside, which are "Marca" and "Gama" enter image description here

These are my Java entities (the toString() is for Omnifaces):

Modelo:

private Marca marca;
private Gama gama;
getters and setters...
@Override
public String toString() {
    return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}

Marca:

getters and setters...
@Override
public String toString() {
    return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}

Gama:

getters and setters...
@Override
public String toString() {
    return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}

Well and this is my managedBean

ModeloBean

@ManagedBean
@ViewScoped
public class ModeloBean {

private Modelo modelo = new Modelo();    
getters and setters ...

//This is for call the DB to retrieve the value, and works fine, but i cant show the preselected value to the xhtml
public void leer(Modelo mo) throws Exception {
    ModeloDAO dao = new ModeloDAO();

    try {
        this.init();           
        this.modelo = dao.leer(mo);            
    } catch (Exception e) {
        throw e;
    } finally {
        dao = null;            
    }
}

This is my xhtml Page I have a dialog which I used it for save and update an object

<p:dialog id="dlgDatos" widgetVar="wdlgDatos" modal="true" appendToBody="true" header="#{modeloBean.accion}" draggable="false" resizable="false">                                                            
                <h:form>
                    <h:panelGrid columns="2">                            
                        <p:outputLabel value="Marca" />
                        <p:selectOneMenu value="#{modeloBean.modelo.marca}" converter="omnifaces.SelectItemsConverter" filter="true" filterMatchMode="startsWith" required="true">
                            <f:selectItem itemLabel="Seleccione" itemValue="#{null}" noSelectionOption="true" />
                            <f:selectItems value="#{marcaBean.lstMarcasVigentes}" var="marca" itemLabel="#{marca.nombre}" itemValue="#{marca}" />
                        </p:selectOneMenu>

                        <p:outputLabel value="Gama" />                            
                        <p:selectOneMenu value="#{modeloBean.modelo.gama}" converter="omnifaces.SelectItemsConverter" filter="true" filterMatchMode="startsWith" required="true">
                            <f:selectItem itemLabel="Seleccione" itemValue="#{null}" noSelectionOption="true" />
                            <f:selectItems value="#{gamaBean.lstGamasVigentes}" var="gama" itemLabel="#{gama.nombre}" itemValue="#{gama}" />
                        </p:selectOneMenu>

                        <p:outputLabel for="txtNombre" value="Modelo" />
                        <p:column>
                            <p:inputTextarea id="txtNombre" value="#{modeloBean.modelo.nombre}" />
                            <p:watermark for="txtNombre" value="Para registrar varios modelos, sepárelos por comas (,)" />
                        </p:column>
                        
                        <p:outputLabel value="Vigencia" rendered="#{modeloBean.accion eq 'Modificar'}"/>
                        <p:selectBooleanCheckbox value="#{modeloBean.modelo.vigencia}" rendered="#{modeloBean.accion eq 'Modificar'}"/>

                        <p:commandButton value="#{modeloBean.accion}" actionListener="#{modeloBean.operar()}" oncomplete="PF('wdlgDatos').hide(); PF('wdtLista').clearFilters();" update=":frmLista:dtLista, :msj"/>
                        <p:commandButton value="Cancelar" immediate="true" onclick="PF('wdlgDatos').hide();"/>
                    </h:panelGrid>
                </h:form>
            </p:dialog>                

The selectOneMenu works fine for save, but for update only retrieve me the Strings value and not the preselected value of my comboBoxes

enter image description here

This is the dialog which only retrieve the String value of "105" cause is a String and my boolean value for the checkbox "Vigencia" but not my comboBoxes values. Where am I wrong?

enter image description here

Was it helpful?

Solution

I solved it adding this to my entites (hashCode and equals)

 @Override
public int hashCode() {
    int hash = 5;
    hash = 83 * hash + this.codigo;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Modelo other = (Modelo) obj;
    if (this.codigo != other.codigo) {
        return false;
    }
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top