hashcode

使用hibernate延迟加载出现下面异常:
LazyInitializationException:19 - illegal access to loading collection org.hibernate.LazyInitializationException: illegal access to loading collection
网上查了下说是hashcode的冲突
pojo类的设计是继承一个基类Base,代码如下:
public class Base implements Serializable {
private static final long serialVersionUID = 5794434135563088443L;
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean equals(Object obj){
if(this==obj) return true;
if(!(obj instanceof Base))
return false;
Base target=(Base)obj;
if(this.getId()!=null&&this.getId()!=0){
return this.getId().equals(target.getId());
}
if(target.getId()!=null&&this.getId()!=0){
return false;
}
return EqualsBuilder.reflectionEquals(this, obj);

}
public int hashCode(){
    if(this.getId()!=null&&this.getId()!=0){
        return this.getId().hashCode();
    }
    return HashCodeBuilder.reflectionHashCode(this);
}

public String toString(){
return ReflectionToStringBuilder.toString(this);
}
}
只要把pojo类的extends Base去掉 就不出现异常了,现在的问题是我怎么为pojo类设计hashcode比较合适

不要用这样反射生成hashcode
这样就把关联的集合也生成hashcode

HashCodeBuilder.reflectionHashCode(this); 不要用这种方式

1 从下面的异常看

[quote]LazyInitializationException:19 - illegal access to loading collection org.hibernate.LazyInitializationException: illegal access to loading collection [/quote]

不象是你说的hashCode问题,倒象是你访问一个对象,这个对象是延迟加载的,当你访问这个对象时,session已经关闭了,就抛出这个异常,你顺着这个思路看看,是否是这个引起的。

2
[quote]怎么为pojo类设计hashcode比较合适 [/quote]
“effect java”中有一节讲了设计hashCode的通用方法,可以去参考。