项目闹鬼之hibernate2.1.6

项目用的持久化层是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对象几个主要方法如下:

ExpandedBlockStart.gif ContractedBlock.gif private  Key(Serializable id, Serializable identifierSpace, Class clazz,  boolean  batchLoadable)  dot.gif {
InBlock.gif        
if  (id == null throw   new  AssertionFailure( " null identifier " );
InBlock.gif        
this .identifier = id;
InBlock.gif        
this .identifierSpace  =  identifierSpace;
InBlock.gif        
this .clazz  =  clazz;
InBlock.gif        
this .isBatchLoadable  =  batchLoadable;
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
/** */ /**
InBlock.gif     * Construct a unique identifier for an entity class instance
ExpandedBlockEnd.gif     
*/

ExpandedBlockStart.gifContractedBlock.gif    
public  Key(Serializable id, ClassPersister p)  dot.gif {
InBlock.gif        
this ( id, p.getIdentifierSpace(), p.getMappedClass(), p.isBatchLoadable() );
ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   boolean  equals(Object other)  dot.gif {
InBlock.gif        Key otherKey 
=  (Key) other;
InBlock.gif        
return  otherKey.identifierSpace.equals( this .identifierSpace)  &&  otherKey.identifier.equals( this .identifier);
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
public   int  hashCode()  dot.gif
InBlock.gif        
int  result  =   17 ;
InBlock.gif        result 
=   37   *  result  +  identifierSpace.hashCode();
InBlock.gif        result 
=   37   *  result  +  identifier.hashCode();

这个Key在hiberante几个版本都一样
但在ClassPersister在2.1.6和2.1.7却有不同:
hibernate 2.1.7中在AbstractEntityPersister中

ExpandedBlockStart.gif ContractedBlock.gif public  Serializable getIdentifierSpace()  dot.gif {
InBlock.gif    
return  rootClassName;
ExpandedBlockEnd.gif}

但在hibernate 2.1.6 中的EntityPersister

ExpandedBlockStart.gif ContractedBlock.gif   public  Serializable getIdentifierSpace()  dot.gif {
InBlock.gif  
return  qualifiedTableName;
ExpandedBlockEnd.gif }

问题就出在这里。赶紧把hibernate从2.1.6升级到2.1.8。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值