2

我有一个问题,当我将对象保存到数据库时它工作正常,但是当我想从数据库中检索它时不起作用,我使用的是 Omnifaces 的 selectItemsConverter

我有我的对象“Modelo”,里面有另外两个对象,分别是“Marca”和“Gama” 在此处输入图像描述

这些是我的 Java 实体(toString() 用于 Omnifaces):

型号:

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

马卡:

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

伽马:

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

好吧,这是我的 managedBean

模型豆

@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;            
    }
}

这是我的 xhtml 页面我有一个对话框,我用它来保存和更新对象

<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>                

selectOneMenu 可以很好地保存,但更新只检索我的字符串值而不是我的组合框的预选值

在此处输入图像描述

这是仅检索“105”的字符串值的对话框,原因是字符串和我的复选框“Vigencia”的布尔值,而不是我的组合框值。我哪里错了?

在此处输入图像描述

4

1 回答 1

2

我解决了它,将它添加到我的实体(hashCode 和 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;
}
于 2014-05-05T02:26:04.007 回答