項目用的持久化層是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對象幾個主要方法如下:

private ?Key(Serializable?id,?Serializable?identifierSpace,?Class?clazz,? boolean ?batchLoadable)? {
????????
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中

public ?Serializable?getIdentifierSpace()? {
????
return ?rootClassName;
}

但在hibernate 2.1.6 中的EntityPersister

? public ?Serializable?getIdentifierSpace()? {
??
return ?qualifiedTableName;
?}

問題就出在這里。趕緊把hibernate從2.1.6升級到2.1.8。