我有一個模型類DOModel:java:我可以通過觀察哈希碼來觀察對象上的值更改嗎?
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;
}
}
這個類是在ORM使用,當DOModel的實例的一個或多個屬性發生變化,我需要堅持的DOModel的實例回數據庫。
所以對我來說存在一個問題:我怎麼能知道一個DOModel實例對象與特定時間相比被修改了?
請注意方法dataValueHashCode,我使用所有屬性的哈希碼的組合來衡量,如果一個模型是changed.the基本流程是:
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.
看來,現在的工作原理,但作爲一個java新手,我擔心有潛在的問題。所以在我走之前,我想證明我的方式不會導致我錯誤的地方。
作爲中國人,我的英語不夠好。如果你有問題要了解我在說什麼,請發表評論,我會盡我所能編輯此問題。
非常感謝!
非常感謝你! – CaiNiaoCoder