how to get the last id inserted in JSF and Primefaces?

2

I have a problem in showing the last inserted code and showing it in my web application as shown in the image when I press the new button. It shows me another window and where it says code I have to show P007 but it shows me 'P0061'. I leave the code if you can help me thank you very much.

In the Model

private String id test;  private String name;

public String getIdprueba() {
    return idprueba;
}

public void setIdprueba(String idprueba) {
    this.idprueba = idprueba;
}

public String getNombre() {
    return nombre;
}

In the DAO

public Try to get LastRegister () throws Exception {         ResultSet rs;          Test test = null;         try {

        this.Conectar();
       PreparedStatement st=this.getCn().prepareCall("SELECT Max(idprueba) as idprueba FROM pruebas");
        rs=st.executeQuery();
       if(rs.next()){
           prueba=new Prueba();
           prueba.setIdprueba(rs.getString("idprueba"));

       }

    }catch(Exception e){
       // this.getCn().rollback();
        throw e;
    }finally{
        this.Cerrar();
    }
 return prueba;
}

On the Bean

public void numeracionPrueba()throws Exception{
       PruebaDao dao=new PruebaDao();;
       try{
           this.prueba=dao.obtenerUltimoRegistro();
           this.idprueba=this.prueba.getIdprueba()+1;

       }catch(Exception e){
           throw  e;
       }

   }

In XHTML

 <f:event type="preRenderView" listener="#{pruebaBean.listar('F')}" />     
      <p:growl id="msj" autoUpdate="true" />
            <h:form id="frm">

                <p:commandButton value="Nuevo" oncomplete="PF('wdlgDatos').show();" actionListener="#{pruebaBean.setAccion('Registrar')}"  onclick="#{pruebaBean.numeracionPrueba()}" update=":dlgDatos"/>
                <p:dataTable id="data" value="#{pruebaBean.lstPrueba}" var="prueba">
                    <p:column headerText="Codigo">
                        <p:outputLabel value="#{prueba.idprueba}"/>
                   </p:column>  

            <p:column headerText="descripcion">
                <p:outputLabel value="#{prueba.nombre}"/>
            </p:column>   


                </p:dataTable>
            </h:form>
      <p:dialog header="Registro de ......" widgetVar="wdlgDatos" id="dlgDatos">

           <h:form>
         <p:panelGrid columns="2">

         <p:outputLabel  value="codigo" />
         <p:outputLabel  value="#{pruebaBean.idprueba}"/>

           <p:outputLabel  value="des" />
           <p:inputText  value="#{pruebaBean.prueba.nombre}"/>


        <p:commandButton value="#{pruebaBean.accion}" actionListener="#{pruebaBean.operar()}" oncomplete="PF('wdlgDatos').hide();" update=":frm:data"/>
        <p:commandButton value="Cancelar" immediate="true" oncomplete="PF('wdlgDatos').hide();"/>

        </p:panelGrid>           

        </h:form>
    
asked by Libra2880 09.09.2016 в 05:43
source

6 answers

1

It's very simple. Order descending by code and limit the results to one. In this way, you will always get the last code inserted.

String sql = "SELECT p.codigo FROM Producto p ORDER BY p.codigo DESC";
TypedQuery<String> query = em.createQuery(sql, String.class);
query.setMaxResults(1);
// cuidado, lanza NoResultException si no hay resultados
String lastCode = query.getSingleResult();
    
answered by 09.09.2016 в 13:46
0

If in the debug you can observe the new assigned value. Take a look at the update attribute of the action or button that opens the dialog, panelgrid or whatever, give it an id and refresh it from the action (update attribute) so that update=": idDelElemento"

    
answered by 09.09.2016 в 11:55
0

Regardless of your logic to get the next Id, do not directly update the dialog and include a container such as outputpanel :

<p:dialog header="Registro de ......" widgetVar="wdlgDatos" id="dlgDatos">

      <h:form id="formDialog">

        <p:outputPanel id="output">

           <p:panelGrid columns="2">

               <p:outputLabel  value="codigo" />
               <p:outputLabel  value="#{pruebaBean.idprueba}"/>

               <p:outputLabel  value="des" />
               <p:inputText  value="#{pruebaBean.prueba.nombre}"/>


               <p:commandButton value="#{pruebaBean.accion}" actionListener="#{pruebaBean.operar()}" oncomplete="PF('wdlgDatos').hide();" update=":frm:data"/>
               <p:commandButton value="Cancelar" immediate="true" oncomplete="PF('wdlgDatos').hide();"/>

            </p:panelGrid>           

        </p:outputPanel>

    </h:form>

And update it in the commandButton with something like the following:

update= ": formDialog: output"

Greetings!

    
answered by 27.01.2017 в 17:59
0

Put an id to your form that is inside the dialog: id="idFormRegistro"

<p:dialog header="Registro de ......" widgetVar="wdlgDatos" id="dlgDatos">

           <h:form id="idFormRegistro">
         <p:panelGrid columns="2">

         <p:outputLabel  value="codigo" />
         <p:outputLabel  value="#{pruebaBean.idprueba}"/>

           <p:outputLabel  value="des" />
           <p:inputText  value="#{pruebaBean.prueba.nombre}"/>


        <p:commandButton value="#{pruebaBean.accion}" actionListener="#{pruebaBean.operar()}" oncomplete="PF('wdlgDatos').hide();" update=":frm:data"/>
        <p:commandButton value="Cancelar" immediate="true" oncomplete="PF('wdlgDatos').hide();"/>

        </p:panelGrid>           

        </h:form>

and on your new button would be as follows:

<p:commandButton value="Nuevo" oncomplete="PF('wdlgDatos').show()" actionListener="#{pruebaBean.setAccion('Registrar')}"  onclick="#{pruebaBean.numeracionPrueba()}" update=":idFormRegistro"/>
    
answered by 18.04.2017 в 03:17
0

The problem is in your XHTML, you must update the dialog but in the form put an id to the form of the dialog so <h:form id="formDialog"> and in the button that sends to call the dialog add in the update the id of the form formDialog <p:commandButton value="Nuevo" oncomplete="PF('wdlgDatos').show();" actionListener="#{pruebaBean.setAccion('Registrar')}" onclick="#{pruebaBean.numeracionPrueba()}" update=":formDialog"/>

 <f:event type="preRenderView" listener="#{pruebaBean.listar('F')}" />     
      <p:growl id="msj" autoUpdate="true" />
            <h:form id="frm">

                <p:commandButton value="Nuevo" oncomplete="PF('wdlgDatos').show();" actionListener="#{pruebaBean.setAccion('Registrar')}"  onclick="#{pruebaBean.numeracionPrueba()}" update=":formDialog"/>
                <p:dataTable id="data" value="#{pruebaBean.lstPrueba}" var="prueba">
                    <p:column headerText="Codigo">
                        <p:outputLabel value="#{prueba.idprueba}"/>
                   </p:column>  

            <p:column headerText="descripcion">
                <p:outputLabel value="#{prueba.nombre}"/>
            </p:column>   


                </p:dataTable>
            </h:form>
      <p:dialog header="Registro de ......" widgetVar="wdlgDatos" id="dlgDatos">

           <h:form id="formDialog">
         <p:panelGrid columns="2">

         <p:outputLabel  value="codigo" />
         <p:outputLabel  value="#{pruebaBean.idprueba}"/>

           <p:outputLabel  value="des" />
           <p:inputText  value="#{pruebaBean.prueba.nombre}"/>


        <p:commandButton value="#{pruebaBean.accion}" actionListener="#{pruebaBean.operar()}" oncomplete="PF('wdlgDatos').hide();" update=":frm:data"/>
        <p:commandButton value="Cancelar" immediate="true" oncomplete="PF('wdlgDatos').hide();"/>

        </p:panelGrid>           

        </h:form>**texto en negrita**
    
answered by 26.06.2017 в 16:57
0

You are concatenating to a String (test) another String (1) in the following code

this.idprueba=this.prueba.getIdprueba()+1;

Given in test id are stored text values such as:

  • P001
  • P002
  • P003
  • ...

You should extract the whole part of that code and treat it as a number to be able to increase by one unit.

    
answered by 27.09.2017 в 10:32