I use composite-id,the composite-id is composed by id and uid,like follows:
Test.hbm.xml
Code:
<class name="Test.Model" table="Test" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
<composite-id mapped="false" unsaved-value="undefined">
<key-property name="id" column="id" type="string" />
<key-property name="uid" column="uid" type="string" />
</composite-id>
</class>
Model.java is follows:
Code:
package Test;
public class Model{
private String id;
private String uid;
public String getId(){
return id;
}
public void setId(String id){
this.id=id;
}
public String getUid(){
return uid;
}
public void setUid(String uid){
this.uid=uid;
}
public boolean equals(Object obj) {
if(!(obj instanceof Model)){
return false;
}
Model mm=(Model)obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(this.id, mm.id)
.append(this.uid, mm.uid)
.isEquals();
}
public int hasCode(){
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(this.id)
.append(this.uid)
.toHashCode();
}
}
I have two question,
Question 1:
When I start Tomcat,tomcat raise following warning:
composite-id class does not override hashCode():Test.Model
I have realize hasCode function,why tomcat raise above warning message? How to correct my code to get rid of warning message?
Question 2:
I have two record in my database,
id uid
-----------
11 Rose
12 Kate
I want to delete id='11' record,I use following code:
Code:
Model mm=new Model();
mm.setId('11');
this.getHibernateTemplate().delete(mm);
When execute above code,I find id='11' record still in database,why it can't be deleted from database? I am puzzled with it!
Then I modify above code,like follows:
Code:
Model mm=new Model();
mm.setId('11');
List info=this.getHibernateTemplate().find("from Test.Model where id=11");
mm=(Model)info.get(0);
this.getHibernateTemplate().delete(mm);
This time the record of id='11' has been deleted from database. Why? Why I must use this.getHibernateTemplate().find first,then I can use this.getHibernateTemplate().delete?
Any idea will be appreciated!
Best regards,
Edward