简体   繁体   中英

Autocomplete Primefaces not record to database

I'm using:

  • Primefaces 4.0
  • Netbeans 8.0.1
  • Omnifaces 2.1
  • JSF 2.2
  • Hibernate 4.3.1

and I implement a Autocomplete in primefaces, the view shows the list from database, but at the moment to record in the database, the result is that the id is null

here my code:

<h:form id="formInsertar">
            <p:messages autoUpdate="true"/>
            <p:panelGrid columns="2">
                <p:outputLabel value="Id:" />
                <p:inputText value="#{usuarioBean.usuario.id}" disabled="true"/>
                <p:outputLabel value="Nombre:" />
                <p:inputText value="#{usuarioBean.usuario.nombre}" />
                <p:outputLabel value="Buscar x tipo" for="tipo"/>
                <p:autoComplete id="tipo" value="#{usuarioBean.usuario.tipousuario}" converter="omnifaces.SelectItemsConverter"
                                completeMethod="#{tipoUsuarioBean.completarTipo}" var="t"
                                itemLabel="#{t.nombre}" itemValue="#{t}">

                </p:autoComplete>
                <p:outputLabel value="Login:" />
                <p:inputText value="#{usuarioBean.usuario.login}" />
                <p:outputLabel value="Clave:" />
                <p:inputText value="#{usuarioBean.usuario.clave}" />
                <p:outputLabel value="eMail:" />
                <p:inputText value="#{usuarioBean.usuario.email}" />
            </p:panelGrid>
            <p:commandButton value="Crear" update=":formMostrar" actionListener="#{usuarioBean.insertar()}" 
                             />
        </h:form>

My Dao:

@Override
public List<Tipousuario> buscarxNombre(String nombre) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(Tipousuario.class);
    if (StringUtils.isNotBlank(nombre)){
        criteria.add(Restrictions.ilike("nombre",nombre.toUpperCase(),MatchMode.ANYWHERE));
    }
    return criteria.list();
}

My Bean:

public List<Tipousuario> completarTipo(String nombre){
    TipoUsuarioDao tipodao = new TipoUsuarioDaoImp();
    tipousuarios = tipodao.buscarxNombre(nombre);
    return tipousuarios;
}

And the error:

WARN: SQL Error: 515, SQLState: 23000 ERROR: No se puede insertar el valor NULL en la columna 'tipousuario_id', tabla 'miApp.dbo.usuario'. La columna no admite valores NULL. Error de INSERT. Información: could not execute statement

Please I need some help. Thanks

PD: Sorry for my bad english.

Well I solved it. Next steps:

  • The property correct to autocomplete is ListConverter not SelectItemsConverter
  • The way to instead is below:

     <p:outputLabel value="Buscar x tipo" for="tipo"/> <p:autoComplete id="tipo" value="#{usuarioBean.usuario.tipousuario}" completeMethod="#{tipoUsuarioBean.completarTipo}" var="t" itemLabel="#{t.nombre}" itemValue="#{t}" forceSelection="true"> <o:converter converterId="omnifaces.ListConverter" list="#{tipoUsuarioBean.tipousuarios}"/> <p:ajax event="itemSelect" process="@form" /> </p:autoComplete> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM