组件作为 Map 的索引


编写作为索引的 类 必须重写 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)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值