Note that there are some explanatory texts on larger screens.

plurals
  1. POjava :Can I observe value change on object by watching hashcode?
    text
    copied!<p>I have a Model class <strong>DOModel</strong> :</p> <pre><code>package amarsoft.dbmp.credit.web.model; import ejp.annotations.ConcreteTableInheritance; import amarsoft.rcp.base.databinding.BindableModel; @ConcreteTableInheritance public class DOModel extends BindableModel { /** * 编号 */ private String id; /** * 名称 */ private String name; /** * 模板类型,没有太大意义 */ private String type; /** * 模板参数 */ private String args; private String updateTable; private String updateWhere; private String fromClause; private String whereClause; private String groupClause; private String orderClause; public String getId() { return id; } public void setId(String id) { this.firePropertyChange("id", this.id, this.id = id); } public String getName() { return name; } public void setName(String name) { this.firePropertyChange("name", this.name, this.name = name); } public String getType() { return type; } public void setType(String type) { this.firePropertyChange("type", this.type, this.type = type); } public String getArgs() { return args; } public void setArgs(String args) { this.firePropertyChange("args", this.args, this.args = args); } public String getUpdateTable() { return updateTable; } public void setUpdateTable(String updateTable) { this.firePropertyChange("updateTable", this.updateTable, this.updateTable = updateTable); } public String getDoUpdateWhere() { return updateWhere; } public void setDoUpdateWhere(String doUpdateWhere) { this.firePropertyChange("updateWhere", this.updateWhere, this.updateWhere = doUpdateWhere); } public String getFromClause() { return fromClause; } public void setFromClause(String fromClause) { this.firePropertyChange("fromClause", this.fromClause, this.fromClause = fromClause); } public String getWhereClause() { return whereClause; } public void setWhereClause(String whereClause) { this.firePropertyChange("whereClause", this.whereClause, this.whereClause = whereClause); } public String getGroupClause() { return groupClause; } public void setGroupClause(String groupClause) { this.firePropertyChange("groupClause", this.groupClause, this.groupClause = groupClause); } public String getOrderClause() { return orderClause; } public void setOrderClause(String orderClause) { this.firePropertyChange("orderClause", this.orderClause, this.orderClause = orderClause); } @Override public String toString() { return "DOModel [id=" + id + ", name=" + name + "]"; } @Override public int dataValueHashCode() { int code = 0; if (id != null) { code += id.hashCode(); } if(name != null){ code += name.hashCode(); } if(type != null){ code += type.hashCode(); } if(args != null){ code += args.hashCode(); } if(updateTable != null){ code += updateTable.hashCode(); } if(updateWhere != null){ code += updateWhere.hashCode(); } if(fromClause != null){ code += fromClause.hashCode(); } if(whereClause != null){ code += whereClause.hashCode(); } if(groupClause != null){ code += groupClause.hashCode(); } if(orderClause != null){ code += orderClause.hashCode(); } return code; } } </code></pre> <p>this class is used in ORM, when one or more property of DOModel's instance is changed, I need to persist the DOModel's instance back to database.</p> <p>so there is a problem for me: how can I know a DOModel instance object is modified compared to a specific time ?</p> <p>please notice the method <strong>dataValueHashCode</strong>, I use a combination of the hash code of all the properties to measure if a model is changed.the basic flow is:</p> <pre><code>1.load the a DOModel object from database 2.call dataValueHashCode method and cache it 3.(optional)modify property values 4.when need to save the object back to database, call dataValueHashCode method again and compare it to the cached one 5.if match, no change. if not match, save it back to database. </code></pre> <p>It seems that works right now, but as a java newbie, I am worrying there is potential problems. so before I go further, I want to prove my way will not lead me to a wrong place.</p> <p>As a Chinese, my English is not good enough. if you have problem to understand what I am talking about, please post comment, I will try my best to edit this question.</p> <p>thanks a lot!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload