-->
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.  [ 6 posts ] 
Author Message
 Post subject: ClassCastException: this.getHibernateTemplate().find(".
PostPosted: Mon Sep 05, 2005 8:55 am 
Newbie

Joined: Mon Sep 05, 2005 8:40 am
Posts: 2
I'm using hibernate3 in combination with spring.

I have the following problem: I have a many-to-many relationship between product and publisher;

Product.hbm.xml:

Code:

<class name="com.sonydadc.claudio.product.beans.BasicProduct" table="CLAUDIO_PRODUCT" mutable="true" lazy="true">
  <id name="id" column="PRODUCT_ID" type="string"/id>

  <!--properties-->

  <set name="publishers" table="CLAUDIO_PRODUCT_PUBLISHER" lazy="true" inverse="true" cascade="all">
         <key column="PRODUCT_ID" />
         <one-to-many class="com.sonydadc.claudio.product.beans.ProductPublisher"/>
   </set>

</class>




ProductPublisher.hbm.xml:

Code:
<class name="com.sonydadc.claudio.product.beans.ProductPublisher" table="CLAUDIO_PRODUCT_PUBLISHER">       
       
        <composite-id>
      <key-many-to-one name="product"  class="com.sonydadc.claudio.product.beans.BasicProduct" column="PRODUCT_ID" />
           <key-many-to-one name="publisher" class="com.sonydadc.claudio.product.beans.Publisher" column="PUBLISHER_ID" /> 
       </composite-id>
</class>



Publisher.hbm.xml:

Code:
<class name="com.sonydadc.claudio.product.beans.Publisher" table="CLAUDIO_PUBLISHER" lazy="false">       
        <id name="id" column="PUBLISHER_ID" type="string"/>
       
        <!--properties-->
       
        <set name="products" table="CLAUDIO_PRODUCT_PUBLISHER" lazy="true" inverse="true" cascade="all">
         <key column="PUBLISHER_ID"/>
         <one-to-many class="com.sonydadc.claudio.product.beans.ProductPublisher"/>
   </set>
</class> 



All I want is to retrieve all ProductPublisher-Objects (by a given productId) in a DAO-Method:

Code:

public List getProductPublishersByPublisherId(String id) {
  String query = "from ProductPublisher";
  return this.getHibernateTemplate().find(query);
}



When executing the find-Method all I get is the following exception:

Code:
[ERROR] [action] - Servlet.service() for servlet action threw exception <java.lang.ClassCastException: com.sonydadc.claudio.product.beans.ProductPublisher>java.lang.ClassCastException: com.sonydadc.claudio.product.beans.ProductPublisher
   at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:650)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:277)
   at org.hibernate.loader.Loader.doQuery(Loader.java:384)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
   at org.hibernate.loader.Loader.doList(Loader.java:1499)
   at org.hibernate.loader.Loader.list(Loader.java:1482)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:365)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:268)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:782)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:751)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:742)
   at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:734)
   at com.sonydadc.claudio.product.dao.ProductPublisherDAO.getProductPublishersByPublisherId(ProductPublisherDAO.java:37)
   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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:288)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
   at $Proxy11.getProductPublishersByPublisherId(Unknown Source)
   at com.sonydadc.claudio.struts.publish.actions.RemovePublisherAction.execute(RemovePublisherAction.java:42)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
   at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:172)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:595)



Has somebody encountered (and solved) a similar problem.

Thx Stephan


Top
 Profile  
 
 Post subject: Solution
PostPosted: Mon Sep 05, 2005 5:18 pm 
Beginner
Beginner

Joined: Sun May 29, 2005 4:03 pm
Posts: 24
Hi Stefan, I just had the same problem and I forgot to implement
a serializable interface to the m:n relational mapping class....
Every Hibernate Object has to implement serializable...

If anyone from the Hibernat devs is reading this, please add
a better error message to that, because the error probably is caused by
a class cast to serializable which is internally performed...


Top
 Profile  
 
 Post subject: Re: Solution
PostPosted: Mon Sep 05, 2005 8:03 pm 
Newbie

Joined: Mon Aug 29, 2005 1:05 pm
Posts: 3
werpu wrote:
If anyone from the Hibernat devs is reading this, please add
a better error message to that, because the error probably is caused by
a class cast to serializable which is internally performed...

Really, this is a problem that should be solved by Sun, not the Hibernate Team. Whenever a class cast exception is thrown, the exception should include a message saying: "Tried to cast an object of type <Actual Type> to class <Expected Type>".


Top
 Profile  
 
 Post subject: Both are to blame
PostPosted: Mon Sep 05, 2005 8:14 pm 
Beginner
Beginner

Joined: Sun May 29, 2005 4:03 pm
Posts: 24
First sun for not sending an better error message, but it would be easier to get a fix on the hibernate side for this problem, by simply sending a hibernate exception with a single instanceof query upfront...

something like (HibernateException ... object of type ... does not implement the Serializable interface, therefore the mapping fails)

anyway, the error is documented now in the forum, just in case somebody
runs into the same problem also and spends hours looking for a solution.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 7:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Provide a patch, I've opened HHH-938

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Ok thanks
PostPosted: Wed Sep 07, 2005 6:59 pm 
Beginner
Beginner

Joined: Sun May 29, 2005 4:03 pm
Posts: 24
emmanuel wrote:
Provide a patch, I've opened HHH-938


I will check it out on the weekend, I am sort of new to the Hibernate sourcecode, but if I can help...

;-)

Expect my patch around monday, if I can pull it off, which I should be able to.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.