JSF - How to get old and new value when using Primefaces RowEditEvent?

11,152

I would recommend not having name be your primary key, but instead add an auto-incrementing id field and make that your primary key, and don't make that field editable by the user. This guarantees you always have access to the record that you want to update.

Generally speaking it's bad practice to attempt to modify the primary key in a database the way you're trying to do it.

Share:
11,152
Catfish
Author by

Catfish

Old enough to party. Nerdy by nature. Codementor Blog

Updated on June 30, 2022

Comments

  • Catfish
    Catfish almost 2 years

    I have a datatable with 2 columns(name and description) and I want to update my database when a person updates a row and clicks the checkmark which triggers the execBacking.update method.

    In this method, I get the new values by casting event.getObject(), but how can i also get the old values? The name is my primary key so i need the old value to know which row to update in the db.

    xhtml page

      <p:ajax event="rowEdit" listener="#{execBacking.update}" update=":execForm:execMessages" /> 
    
      <p:column headerText="Name">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{exec.name}" />
            </f:facet>
            <f:facet name="input">
                <h:inputText value="#{exec.name}" />
            </f:facet>
        </p:cellEditor>
      </p:column>
    
      <p:column headerText="Description">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{exec.description}" />
            </f:facet>
            <f:facet name="input">
                <h:inputText value="#{exec.description}" />
            </f:facet>
        </p:cellEditor>
      </p:column>
    
      <p:column headerText="Actions">
        <p:rowEditor />
        <p:commandLink styleClass="ui-icon ui-icon-trash" type="submit" actionListener="#{execBacking.delete}" update=":execForm:execMessages" >
                <f:attribute name="execName" value="#{exec.name}" />
            </p:commandLink>
      </p:column>
    </p:dataTable>
    

    Backing bean

    public void update(RowEditEvent event) {
    
            Dao dao = new Dao(ds);
            Exec exec = (Exec) event.getObject();
            System.out.println("name = "+exec.getName());  //New name
            System.out.println("desc = "+exec.getDescription());  //New desc
            try {
                // If the new exec was updated successfully
                if(dao.updateExec(accessBacking.getUsername(), null, exec.getName(), exec.getDescription())) {
                    FacesContext.getCurrentInstance().addMessage("growl", new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Exec Updated Successfully!"));
                } else {
                    FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error Updating Exec!"));
                }
            } catch (Exception e) {
                FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage()));
            }
        }
    
  • Catfish
    Catfish over 10 years
    Can't change the database. There's a 15 year old perl app that i'm rewriting and the primary key is the name. I know it's not the best practice, but it's what I've got to work with.
  • StormeHawke
    StormeHawke over 10 years
    Ok... plan B then would be to simply store the old name in a second variable and handle it that way
  • Catfish
    Catfish over 10 years
    Ideally I guess i'd like to somehow pass the original value via an f:attribute or f:param or something like that rather than adding a duplicate "name" to my Exec object.
  • StormeHawke
    StormeHawke over 10 years
    End result is you'll have to duplicate it somewhere. If it were me I'd just add an "originalName" field to the Object. Best advice I can give you. It can be a transient value
  • Catfish
    Catfish over 10 years
    But if I use a valueChangeListener, It won't work with <p:ajax event="rowEdit" listener="#{backing.update}" /> will it?
  • kolossus
    kolossus over 10 years
    @Catfish - I can't say for sure, but I can't think of a reason for it not to