【发布时间】:2013-10-19 01:36:38
【问题描述】:
在我的 GWT 应用程序中,必须跟踪对我的休眠对象所做的更改,所以我有这个简单的 POJO 来将修改传输到服务器端,在那里它们将被记录:
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;
}
}
属性oldValue 和newValue 的类型为Serializable,因此可以存储我的整数、字符串、日期和布尔值,以及其他几个Hibernate 对象。
通过使用记录修改的特殊设置方法来实现跟踪(例如:使用setFirstNameLog() 而不是下面的setFirstName()):
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;
}
}
当ModifiedValueReference对象通过GWT RPC到达服务器端时,oldValue和newValue是空的! 为什么?
这些字段在客户端用字符串填充。在服务器端,它们不是 null,而是空字符串。
【问题讨论】:
-
也许更多的代码会有所帮助。可能是实际的服务调用和服务器端实现。
-
@enrybo 感谢您的建议。由于该代码正在工作,因此我在家中制作了一个示例 GWT 项目以尝试重现该问题。我在家的代码运行良好,所以我怀疑工作中的其他一些代码导致了这个问题。我们有 hibernate 和 gilead 在工作,所以任何事情都可能导致问题。如果你愿意,我可以把我现在写的代码贴出来。
-
@enrybo,我想通了。我没有
modifications的设置器,这导致它无法被服务器端的 GWT RPC 重建。
标签: gwt serialization gwt-rpc