mardi 2 juin 2015

adding a list of objects in database

I need your help in my problem. I am using ui:repeat with an inputText to add to my database a list of objects in at the same time . so I call in the beginning a method "champs" to construct a list where I put my objects and then I add them by calling a method "traitementAjout".this is some code snapshots:

   <p:inputText id="Input7" value="#{typePrimeManager.n}" label="nbre d'entree"     required="true" />
    <p:commandButton value="ok" action="#{typePrimeManager.champs()}" update=":form:repeatBlock"/>
    <h:panelGrid id="repeatBlock">
    <ui:repeat value="#{typePrimeManager.last}" varStatus="loop"> 
    <h:inputText value="#{typePrimeManager.last[loop.index]}" />
    </ui:repeat> 
    </h:panelGrid>
    <p:commandButton value="ajouter" action="#{typePrimeManager.traitementAjout}" update=":form:cars1"/> 
    <p:commandButton value="annuler" />

there is method codes:

public void champs()
    {
        last= new String[n];
        for(int i=0;i<n; i++)
        last[i]="";

    }

    public void traitementAjout(){


        //la list des echelons a ajouter avec un type prime
        for(int j=0;j<=n;j++) 
        {

            TypPrime r = new TypPrime();
            r.setIntitulePrime(last[j]);
            TPS.ajouterTypeprime(r);
            List<TypPrime> m = TPS.findByQuery("where Intitule_prime='"+last[j]+"'");
            TypPrime h= new TypPrime();
            h=m.get(0);
TypPrimeEchelonId l= new TypPrimeEchelonId();

            l.setTypPrimeIdPrime(h.getIdPrime());
            l.setEchelonIdEchelon(echelonPrime.getIdEchelon());
            TypPrimeEchelon p= new TypPrimeEchelon();
            p.setId(l);
            p.setEchelon(echelonPrime);
            p.setTypPrime(h);
            TPES.ajouterTypePrimeEchelon(p);
            }


        selectedtype=null;
        RequestContext context = RequestContext.getCurrentInstance();

        context.execute("PF('ajoutertypeprime').hide()");
        RequestContext.getCurrentInstance().update("cars1");


        FacesContext co = FacesContext.getCurrentInstance();
        co.addMessage(null, new FacesMessage("Type de prime Bien ajouté"));
        last=null;  
        }

but when I want to add 2 elements for example I get this error:

Avertissement: #{typePrimeManager.traitementAjout}:java.lang.ArrayIndexOutOfBoundsException: 2 javax.faces.FacesException: #{typePrimeManager.traitementAjout}: java.lang.ArrayIndexOutOfBoundsException: 2

Aucun commentaire:

Enregistrer un commentaire