Search code examples
jsfprimefacesdatatableaudit

How to check if instance entity has been modify in the view?


I need a guide here. Let's say that I'm fetching some records from db an I'm populating a ui datatable with those records.

The datatable can modify the values of the records as its columns are inputs.

<p:dataTable value="#{videogames.videogameList}" var="vg"  >
        <p:column headerText="Name">
            <p:inputText value="#{vg.name}" />
        </p:column>
</p:dataTable>

How can I know if some row was modify in order to update the value in the database?

Should I check each element in the list and compare it to a temporal arraylist with the original values? Is there a more "elegant" way to do this?

Do you know any tutorial or docs that can help me to learn all this.

Thanks!

I am working with Java, JSF, primafaces and JPA.


Solution

  • I think I found a solution for this case.

    What I did, was to add an ajax listener with event="change", to the inputtext that can modify the value of the row, so everytime the value changes, the listener will pass the row as a param to a method, and the method will add the row to a list:

    videogames.xhtml

        <p:column headerText="Nombre">
            <p:inputText value="#{vj.nombre}">
               <!--this is the added listener-->
               **<f:ajax event="change" listener="#{videojuegos.rowSelectedTest(vj)}" />**
            </p:inputText>
        </p:column>
    
       <!-- **Button that calls the method for updating the modify entities columns**-->
       <p:commandButton  value="Actualizar" actionListener="#{videojuegos.updateVideojuego()}"/>
    

    The managed bean receives it, and adds it to a list:

    VideoGamesManagedBean.java

     List  <Videojuego> videojuegoUpdate = new ArrayList<Videojuego>();
    //adds the modify row to a list
    public void rowSelectedTest(Videojuego videojuego){
         videojuegoUpdate.add(videojuego);
     }
    
    //calls the method in the EJB for merging each object in the list
    public void updateVideojuego(){
    
         if(videojuegoUpdate != null && !videojuegoUpdate.isEmpty() ){
            vjbean.mergeVideojuegoList(videojuegoUpdate);
             //after merging, cleans up the arraylist.
             videojuegoUpdate.clear();
         }
     }
    

    and last, the EJB iterates through the list and merge each object

    VideoGameBean.java

    @PersistenceContext
    private EntityManager em;
    
    public void mergeVideojuegoList(List<Videojuego> listVideojuegoUpdate){
       for(Videojuego vjUpdate : listVideojuegoUpdate ){
           em.merge(vjUpdate);
       }
    

    }

    NOTE:

    the only "problem" for this is, if I modify several times the same column before I hit the commandbutton "Actualizar", the list will grow up with the same row. But the merge method will take the last "version" of the column. So for our needs, it is ok.

    Thanks for the previous answers, regards!!

    If you know any better method or find any corner case, let me know!!