-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: ClassCastException getting an object using hibernateTemplate
PostPosted: Tue Dec 12, 2006 10:54 pm 
Newbie

Joined: Tue Dec 12, 2006 10:29 pm
Posts: 4
Getting a weird classcastexception while casting the object which has many-to-one relationship with the object being retrieved. Spring's hibernateTemplate is being used here.

I tried the following but it did not work:
1. Added lazy="no-proxy" to the many-to-one relation
2. Added proxy="com.sterling.drug.common.domain.AlcoholTestResultSubject" to the AlcoholTestResultSubject.hbm.xml
3. Tried with hibernate version 3.0

I have given the additional details here. Thanks in advance.

Hibernate version:[3.2.0 cr4]

Mapping documents:[
AlcoholScreeningResult.hbm.xml
<hibernate-mapping>
<class name="com.sterling.drug.common.domain.AlcoholScreeningResult" table="AlcoholScreeningResult" schema="dbo" catalog="sttestdb">
<id name="alcoholScreeningResultId" type="int">
<column name="AlcoholScreeningResultId" />
<generator class="identity" />
</id>
<many-to-one name="alcoholTestResultSubject" class="com.sterling.drug.common.domain.AlcoholTestResultSubject" fetch="select">
<column name="AlcoholTestResultId" not-null="true" />
</many-to-one>
<property name="orderNumber" type="string">
<column name="OrderNumber" length="20" />
</property>
..........


AlcoholTestResultSubject.hbm.xml
<hibernate-mapping>
<class name="com.sterling.drug.common.domain.AlcoholTestResultSubject" table="AlcoholTestResultSubject" schema="dbo" catalog="sttestdb"
proxy="com.sterling.drug.common.domain.AlcoholTestResultSubject">
<id name="alcoholTestResultId" type="int">
<column name="AlcoholTestResultId" />
<generator class="identity" />
</id>
<many-to-one name="randomListDetail" class="com.sterling.drug.common.domain.RandomListDetail" lazy="false" fetch="select" cascade="all">
<column name="RandomListDetailId" />
</many-to-one>
<many-to-one name="subjectCategory" class="com.sterling.drug.common.domain.SubjectCategory" fetch="select">
<column name="SubjectId" />
<column name="SubjectType" length="20" />
<column name="SubjectSubTypeId" />
</many-to-one>
<property name="employeeId" type="string">
/......

]

Code between sessionFactory.openSession() and session.close():[asr = (AlcoholScreeningResult) getHibernateTemplate().get(AlcoholScreeningResult.class, pkAlcoholScreeningResult);]

Full stack trace of any exception that occurs:[2006-12-12 11:55:20,557 INFO [STDOUT] 11:55:20,542 ERROR [AlcoholTestResultDAOImpl] processAlcoholConfirmationResult failed
java.lang.ClassCastException: com.sterling.drug.common.domain.AlcoholTestResultSubject$$EnhancerByCGLIB$$489650c7_3
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:106)
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.getProxy(CGLIBProxyFactory.java:49)
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3232)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:237)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:174)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:839)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2821)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:370)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:122)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:178)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:801)
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:460)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:454)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:448)
at com.sterling.drug.common.dao.AlcoholTestResultDAOImpl.getAlcoholScreeningResultById(AlcoholTestResultDAOImpl.java:314)
at com.sterling.drug.common.dao.AlcoholTestResultDAOImpl.processAlcoholConfirmationResult(AlcoholTestResultDAOImpl.java:288)
at com.sterling.drug.alcoholresult.bo.AlcoholTestResultBO.processAlcoholConfirmationResult(AlcoholTestResultBO.java:59)
at com.sterling.drug.alcoholresult.bo.AlcoholTestResultBO$$FastClassByCGLIB$$3e1094dd.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:714)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:652)
at com.sterling.drug.alcoholresult.bo.AlcoholTestResultBO$$EnhancerByCGLIB$$3181a7a8.processAlcoholConfirmationResult(<generated>)
at com.sterling.drug.alcoholresult.manager.AlcoholTestResultManager.processAlcoholConfirmationResult(AlcoholTestResultManager.java:88)
at com.sterling.drug.alcoholresult.service.AlcoholConfirmationResultServiceImpl.processAlcoholConfirmationResult(AlcoholConfirmationResultServiceImpl.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.xfire.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:54)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler.sendMessage(ServiceInvocationHandler.java:271)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler$1.run(ServiceInvocationHandler.java:84)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler.execute(ServiceInvocationHandler.java:132)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:107)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.transport.DefaultEndpoint.onReceive(DefaultEndpoint.java:64)
at org.codehaus.xfire.transport.AbstractChannel.receive(AbstractChannel.java:38)
at org.apache.servicemix.jsr181.Jsr181ExchangeProcessor.doProcess(Jsr181ExchangeProcessor.java:113)
at org.apache.servicemix.jsr181.Jsr181ExchangeProcessor.process(Jsr181ExchangeProcessor.java:68)
at org.apache.servicemix.common.AsyncBaseLifeCycle.processExchange(AsyncBaseLifeCycle.java:410)
at org.apache.servicemix.common.BaseLifeCycle.onMessageExchange(BaseLifeCycle.java:43)
at org.apache.servicemix.jbi.messaging.DeliveryChannelImpl.processInBound(DeliveryChannelImpl.java:624)
at org.apache.servicemix.jbi.nmr.flow.AbstractFlow.doRouting(AbstractFlow.java:170)
at org.apache.servicemix.jbi.nmr.flow.seda.SedaFlow.doRouting(SedaFlow.java:177)
at org.apache.servicemix.jbi.nmr.flow.seda.SedaQueue$1.run(SedaQueue.java:227)
at org.apache.geronimo.connector.work.WorkerContext.run(WorkerContext.java:291)
at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Thread.java:595)
]

Name and version of the database :[MS SQL Server 2005]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 4:28 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
get may return a Proxy (which is what you get here). Use session.load(..) or hibernateTemplate.load(..) instead.

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 2:29 pm 
Newbie

Joined: Tue Dec 12, 2006 10:29 pm
Posts: 4
I changed get to load as you suggested.
I am getting ClassCastException on the AlcoholScreeningResult object now instead of the AlcoholTestResultSubject .
Here is the stack trace:

java.lang.ClassCastException: com.sterling.drug.common.domain.AlcoholScreeningResult$$EnhancerByCGLIB$$38d63e69_3
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:106)
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.getProxy(CGLIBProxyFactory.java:49)
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3232)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:237)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:174)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:781)
at org.springframework.orm.hibernate3.HibernateTemplate$3.doInHibernate(HibernateTemplate.java:498)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
at org.springframework.orm.hibernate3.HibernateTemplate.load(HibernateTemplate.java:492)
at org.springframework.orm.hibernate3.HibernateTemplate.load(HibernateTemplate.java:486)
at com.sterling.drug.common.dao.AlcoholTestResultDAOImpl.getAlcoholScreeningResultById(AlcoholTestResultDAOImpl.java:314)
at com.sterling.drug.common.dao.AlcoholTestResultDAOImpl.processAlcoholConfirmationResult(AlcoholTestResultDAOImpl.java:288)
at com.sterling.drug.alcoholresult.bo.AlcoholTestResultBO.processAlcoholConfirmationResult(AlcoholTestResultBO.java:59)
at com.sterling.drug.alcoholresult.bo.AlcoholTestResultBO$$FastClassByCGLIB$$3e1094dd.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:714)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:652)
at com.sterling.drug.alcoholresult.bo.AlcoholTestResultBO$$EnhancerByCGLIB$$3181a7a8.processAlcoholConfirmationResult(<generated>)
at com.sterling.drug.alcoholresult.manager.AlcoholTestResultManager.processAlcoholConfirmationResult(AlcoholTestResultManager.java:88)
at com.sterling.drug.alcoholresult.service.AlcoholConfirmationResultServiceImpl.processAlcoholConfirmationResult(AlcoholConfirmationResultServiceImpl.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.xfire.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:54)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler.sendMessage(ServiceInvocationHandler.java:271)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler$1.run(ServiceInvocationHandler.java:84)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler.execute(ServiceInvocationHandler.java:132)
at org.codehaus.xfire.service.binding.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:107)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.transport.DefaultEndpoint.onReceive(DefaultEndpoint.java:64)
at org.codehaus.xfire.transport.AbstractChannel.receive(AbstractChannel.java:38)
at org.apache.servicemix.jsr181.Jsr181ExchangeProcessor.doProcess(Jsr181ExchangeProcessor.java:113)
at org.apache.servicemix.jsr181.Jsr181ExchangeProcessor.process(Jsr181ExchangeProcessor.java:68)
at org.apache.servicemix.common.AsyncBaseLifeCycle.processExchange(AsyncBaseLifeCycle.java:410)
at org.apache.servicemix.common.BaseLifeCycle.onMessageExchange(BaseLifeCycle.java:43)
at org.apache.servicemix.jbi.messaging.DeliveryChannelImpl.processInBound(DeliveryChannelImpl.java:624)
at org.apache.servicemix.jbi.nmr.flow.AbstractFlow.doRouting(AbstractFlow.java:170)
at org.apache.servicemix.jbi.nmr.flow.seda.SedaFlow.doRouting(SedaFlow.java:177)
at org.apache.servicemix.jbi.nmr.flow.seda.SedaQueue$1.run(SedaQueue.java:227)
at org.apache.geronimo.connector.work.WorkerContext.run(WorkerContext.java:291)
at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Thread.java:595)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.