hibernate中提倡持久类实现equals()和hashCode()分析

我们知道,在hibernate中如果有一对多或者多对多的关联时,在po中使用set来存储

原因是set能保证key不能重复

而在数据库中的记录行则对应一个po对象。

比如数据库中行

+--------+-----------+----------+----------+------+------+------------+---------+----------------------------------------------+
| userId | loginName | userName | password | sex  | age  | createDate | isAdmin | content                                      |
+--------+-----------+----------+----------+------+------+------------+---------+----------------------------------------------+
|      1 | leip      | 雷盼     | 1        |    1 |   27 | 2016-04-17 |       1 | 1111                                         |
|    129 | zhangwp   | 张文苹   | 1        |    0 |   26 | 2016-04-17 |       1 | 张文苹结婚了,生了一个大胖小子,取名雷子诺。 |
|    132 | leip-a    | 雷朋     | 1        |    1 |   55 | 2016-04-17 |       1 | 啊都发生                                     |
|    133 | leipp     | 雷胖胖   | 1        |    1 |    1 | 2016-04-21 |       1 | 111                                          |
+--------+-----------+----------+----------+------+------+------------+---------+----------------------------------------------+
则表示有4个user对象。

那么如果数据库中多了一条

134 | leip      | 雷盼     | 1        |    1 |   27 | 2016-04-17 |       1 | 1111                                         |

的记录时,再用hibernate查询出user集合时,如果用户没有重写user的equals和hashcode方法,那么

hibernate会默认的认为userid=1的user和userid=134的user是不同的user,因为object类默认equals是通过对象的地址:

public boolean equals(Object obj) {
        return (this == obj);
    }

而这却不是我们想要的,因为实际业务中,我们会认为这是dirty数据,这2个user是同一个user。

所以hibernate就需要当对于多表关联的数据进行操作时,需要对equals()和hashCode()方法进行重写。

package ray.myHibernate;

public class Student {
	int id;
	String name;
	String age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public int hashCode() {
		super.hashCode();
		final int prime = 31;
		int result = 1;
		result = prime * result + ((age == null) ? 0 : age.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age == null) {
			if (other.age != null)
				return false;
		} else if (!age.equals(other.age))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值