自动完成Primefaces不记录到数据库 [英] Autocomplete Primefaces not record to database

查看:113
本文介绍了自动完成Primefaces不记录到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用:
$ b


  • Primefaces 4.0

  • Netbeans 8.0.1
  • >
  • Omnifaces 2.1

  • JSF 2.2

  • Hibernate 4.3.1


我在primefaces中实现了自动完成功能,该视图显示了数据库中的列表,但是此刻要在数据库中进行记录,结果是该id为null



这里是我的代码:

 < h:form id =formInsertar> 
< 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 tipofor =tipo/>
< p:autoComplete id =tipovalue =#{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 =Crearupdate =:formMostraractionListener =#{usuarioBean.insertar()}
/>
< / h:表格>

我的道:

  @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();

$ / code>

我的Bean:

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

与错误:


WARN:SQL错误:515,SQLState:23000
错误:没有插入插入的字符,空的字符串为'tipousuario_id',tabla'miApp.dbo.usuario'。拉columna没有参加valores NULL。错误德INSERT。
Información:无法执行语句

请给我一些帮助。谢谢



PD:对不起我的英文不好。

我解决了它。
接下来的步骤:


  • 正确自动完成的属性是 ListConverter 不是 SelectItemsConverter
  • c $ c>< p:outputLabel value =Buscar x tipofor =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 =itemSelectprocess =@ form/>
    < / p:autoComplete>



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>
    

这篇关于自动完成Primefaces不记录到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆