同步自:http://www.blogjava.net/AndersLin/archive/2006/07/28/60510.html
项目用的持久化层是hibernate 2.1.6.
前不久出现一个错误,简单描述一下:
现有3个对象:Party,TParty和Individual,其中Individual是Party的子类,Party和TParty各自独立,两个对象都映射到表T_Party。
当独立执行Individual person = (Individual)session.load(Individual.class, id)时,系统正确,.
而在同一thread下(OpenSessionInView),先session.find("from TParty"),再Individual person = (Individual)session.load(Individual.class, id)系统报错ClassCastException,经查此时系统返回的是TParty对象。
初步断定是hibernate问题,参看hiberante 2.1.7没有觉的有什么问题(因为自己电脑本地有这个版本,事实证明这是个错误)。折腾了一天,没有发现问题所在,最后只好下载了hibernate2.1.6,立刻发现问题所在。
hibernate在load对象时,会通过getEntity(key)查看是否加载过,而Key对象几个主要方法如下:
if (id == null ) throw new AssertionFailure( " null identifier " );
this .identifier = id;
this .identifierSpace = identifierSpace;
this .clazz = clazz;
this .isBatchLoadable = batchLoadable;
}
/** */ /**
* Construct a unique identifier for an entity class instance
*/
public Key(Serializable id, ClassPersister p) {
this ( id, p.getIdentifierSpace(), p.getMappedClass(), p.isBatchLoadable() );
}
public boolean equals(Object other) {
Key otherKey = (Key) other;
return otherKey.identifierSpace.equals( this .identifierSpace) && otherKey.identifier.equals( this .identifier);
}
public int hashCode() {
int result = 17 ;
result = 37 * result + identifierSpace.hashCode();
result = 37 * result + identifier.hashCode();
这个Key在hiberante几个版本都一样
但在ClassPersister在2.1.6和2.1.7却有不同:
hibernate 2.1.7中在AbstractEntityPersister中
return rootClassName;
}
但在hibernate 2.1.6 中的EntityPersister
return qualifiedTableName;
}
问题就出在这里。赶紧把hibernate从2.1.6升级到2.1.8。