编写作为索引的 类 必须重写 equas() 和 hashCode() 方法
package zhourj.hibernate.list;
/*
* 作为 Map 索引的类
*/
public class FamilyName {
private String first;
private String last;
public FamilyName(){
}
// 初始化全部属性的构造器
public FamilyName(String first, String last){
this.first = first;
this.last = last;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
// 重写 equals 方法
@Override
public boolean equals(Object obj) {
if( this == obj){
return true;
}
if(obj != null && obj.getClass() == FamilyName.class){
FamilyName target = (FamilyName) obj;
if(target.getFirst() == (getFirst()) && target.getLast() == (getLast())){
return true;
}
else{
return false;
}
}
return false;
}
// 重写 hashCode 方法
@Override
public int hashCode() {
return (getFirst().hashCode())*13+ (getLast()).hashCode();
}
}
基本映射文件如下:
<!-- 映射 Map 的索引为组件的测试 -->
<map name="familyPower" table="familyPower">
<key column="person_id"></key>
<composite-map-key class="FamilyName">
<key-property name="first" type="string"></key-property>
<key-property name="last" type="string"></key-property>
</composite-map-key>
<element column="familypower" type="int"></element>
</map>
数据库得到表的结构如下:注意主键 (PRI)