Pregunta

In my GWT app, have to track changes made to my hibernate objects, so I have this simple POJO to transfer modifications to the server side, where they would be logged:

public class ModifiedValueReference implements Serializable {
    private static final long serialVersionUID = 6144012539285913980L;

    private Serializable oldValue;
    private Serializable newValue;

    public ModifiedValueReference() {
        super();
    }

    public ModifiedValueReference(Serializable oldValue, Serializable newValue) {
        this();
        setOldValue(oldValue);
        setNewValue(newValue);
    }

    public Serializable getOldValue() {
        return oldValue;
    }

    public void setOldValue(Serializable oldValue) {
        this.oldValue = oldValue;
    }

    public Serializable getNewValue() {
        return newValue;
    }

    public void setNewValue(Serializable newValue) {
        this.newValue = newValue;
    }

}

The attributes oldValue and newValue are of type Serializable so my Integers, Strings, Dates, and Booleans can be stored, as well as several other Hibernate objects.

Tracking is achieved by using a special setter method that logs the modification (eg: by using setFirstNameLog() instead of setFirstName() below):

public class Person {

    private String firstname;

    private Map<String, ModifiedValueReference> modifications = 
            new HashMap<String, ModifiedValueReference>(15);

    public void addModification(String key, Serializable oldValue, Serializable newValue) {
        if (key != null && !key.isEmpty()) {
            modifications.put(key, 
                    new ModifiedValueReference(oldValue, newValue));
        }
    }

    public void setFirstnameLog(String firstname) {
        addModification("First Name", getFirstname(), firstname);
        setFirstname(firstname);
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;     
    }
    public String getFirstname() {
        return this.firstname;
    }
}

When the ModifiedValueReference object reaches the server side via GWT RPC, the oldValue and newValue are empty! Why?

Those fields were filled with Strings on the client side. On the server side, they are not null, but empty Strings.

¿Fue útil?

Solución

The issue was that the modifications map in my Person class did not have a setter method (eg: A setModifications(Map<String, ModifiedValueReference>) method).

Because of this, GWT RPC was unable to reconstruct the map on the server side when a Person object was saved via a RPC method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top