hibernate 一对多双向关联在多方重写equals和hashCode方法
2012-08-23 11:56
483 查看
@Override
public
boolean equals(Object obj) {
if(this == obj)
return
true;
if(obj ==
null)
return
false;
if(getClass() != obj.getClass())
return
false;
Businessinformation other = (Businessinformation) obj;
if(this.hashCode() != other.hashCode()){
return
false;
}
return
true;
}
@Override
public
int hashCode() {
int result;
result = (id ==
null? 0:id.hashCode());
result = 31*result +
name.hashCode();
return result;
}
(备注:hashCode()里面如果没有name字段,可以多写几个来确定他的唯一性
比如result = 31*result +age.hashCode();result
= 31*result +createTime.hashCode();等等。。)
public
boolean equals(Object obj) {
if(this == obj)
return
true;
if(obj ==
null)
return
false;
if(getClass() != obj.getClass())
return
false;
Businessinformation other = (Businessinformation) obj;
if(this.hashCode() != other.hashCode()){
return
false;
}
return
true;
}
@Override
public
int hashCode() {
int result;
result = (id ==
null? 0:id.hashCode());
result = 31*result +
name.hashCode();
return result;
}
(备注:hashCode()里面如果没有name字段,可以多写几个来确定他的唯一性
比如result = 31*result +age.hashCode();result
= 31*result +createTime.hashCode();等等。。)
相关文章推荐
- Hibernate的持久化类中什么时候需要重写hashcode()和equals()方法
- hibernate中的po类为什么要重写equals和hashcode方法的对话
- Hibernate中用到联合主键的使用方法,为何要序列化,为何要重写hashcode 和 equals 方法
- Hibernate为什么要重写equals和hashCode方法
- equals和hashCode方法重写 以及hibernate的联合主键生成策略的生成类的生成
- ID的生成策略(hibernate的id生成策略,主键类为什么需要实现序列化接口,同时还要重写hashCode()和equals()方法)
- ID的生成策略(hibernate的id生成策略,主键类为什么需要实现序列化接口,同时还要重写hashCode()和equals()方法)
- hibernate中联合主键要同时重写hashCode()和equals()方法
- Hibernate为什么要重写equals和hashCode方法
- Hibernate中为什么要重写equals()和hashCode()这两个方法
- Hibernate中为什么要重写equals方法和hashcode方法
- Hibernate联合主键重写equals和hashCode方法
- 关于hibernate中实体中equals和hashcode方法的重写
- 对于自定义类,重写equals方法和hashCode方法的技巧
- 为什么重写equals方法,一定要重写HashCode方法
- Java为什么要重写hashCode()和equals方法
- Object 开发中常用的重写方法总结toString,equals,hashCode,compareTo
- 建议重写equals方法时也一并重写hashCode方法
- 建立一个对象模型的时候,为什么要重写hashCode与equals方法
- 重写equals方法的同时为什么要重写hashcode方法