-->
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 on PersistentSet.toArray()
PostPosted: Mon Apr 16, 2007 9:42 am 
Newbie

Joined: Mon Apr 09, 2007 12:25 pm
Posts: 19
Location: Boston, MA
Hibernate version:3.1.3

Mapping documents:
Code:
<hibernate-mapping package="com.fmr.sharespoc.domain" default-lazy="false">
   <class name="BeneficiaryDesignation"
      table="beneficiary_designation" lazy="false">
      <id name="id" column="id">
         <generator class="assigned" />
      </id>
      <set name="beneficiaries" inverse="true">
         <key column="beneficiary_designation_id"></key>
         <one-to-many class="Beneficiary" />
      </set>
      <set name="beneficiaryAllocationGroups" inverse="true">
         <key column="beneficiary_designation_id"></key>
         <one-to-many class="BeneficiaryAllocationGroup" />
      </set>
      <many-to-one name="vendor" class="Vendor" column="vendor_id"
         unique-key="true" not-null="true">
      </many-to-one>
      <many-to-one name="confirmation" class="Confirmation"
         column="confirmation_id" unique-key="true" not-null="true">
      </many-to-one>
   </class>
</hibernate-mapping>

Full stack trace of any exception that occurs:
Code:
ERROR org.apache.catalina.core.StandardWrapperValve(253) - Servlet.service() for servlet sharespoc threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
   at com.fmr.sharespoc.domain.BeneficiaryDesignationValidator.validateBeneficiaries(BeneficiaryDesignationValidator.java:25)
   at com.fmr.sharespoc.web.form.BeneficiaryElectionFormValidator.validateUpdateBeneficiaries(BeneficiaryElectionFormValidator.java:34)
   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.webflow.util.DispatchMethodInvoker.invoke(DispatchMethodInvoker.java:103)
   at org.springframework.webflow.action.FormAction.invokeValidatorMethod(FormAction.java:839)
   at org.springframework.webflow.action.FormAction.doValidate(FormAction.java:965)
   at org.springframework.webflow.action.FormAction.bindAndValidate(FormAction.java:566)
   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.webflow.util.DispatchMethodInvoker.invoke(DispatchMethodInvoker.java:103)
   at org.springframework.webflow.action.MultiAction.doExecute(MultiAction.java:136)
   at org.springframework.webflow.action.AbstractAction.execute(AbstractAction.java:203)
   at org.springframework.webflow.engine.AnnotatedAction.execute(AnnotatedAction.java:142)
   at org.springframework.webflow.engine.ActionExecutor.execute(ActionExecutor.java:61)
   at org.springframework.webflow.engine.support.ActionTransitionCriteria.test(ActionTransitionCriteria.java:88)
   at org.springframework.webflow.engine.support.TransitionCriteriaChain.test(TransitionCriteriaChain.java:73)
   at org.springframework.webflow.engine.Transition.canExecute(Transition.java:198)
   at org.springframework.webflow.engine.Transition.execute(Transition.java:211)
   at org.springframework.webflow.engine.TransitionableState.onEvent(TransitionableState.java:112)
   at org.springframework.webflow.engine.Flow.onEvent(Flow.java:572)
   at org.springframework.webflow.engine.impl.RequestControlContextImpl.signalEvent(RequestControlContextImpl.java:207)
   at org.springframework.webflow.engine.impl.FlowExecutionImpl.signalEvent(FlowExecutionImpl.java:214)
   at org.springframework.webflow.executor.FlowExecutorImpl.resume(FlowExecutorImpl.java:238)
   at org.springframework.webflow.executor.support.FlowRequestHandler.handleFlowRequest(FlowRequestHandler.java:115)
   at org.springframework.webflow.executor.mvc.FlowController.handleRequestInternal(FlowController.java:170)
   at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:806)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:736)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
   at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
   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 com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
   at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
   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:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   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)


I am trying to perform a toArray() on a retrieved Set:

Code:
public void validateBeneficiaries(BeneficiaryDesignation designation,
         Errors errors) {
      Beneficiary[] beneficiaries = (Beneficiary[]) designation.getBeneficiaries().toArray();
...
}


If I inspect designation.getBeneficiaries() I see the following:

Quote:
designation.getBeneficiaries()=PersistentSet (id=128)


If I inspect designation.getBeneficiaries().toArray() I see the following:

Quote:
designation.getBeneficiaries().toArray()=Object[4] (id=168)


When I drill into that array I see an array of Beneficiary of length 4.

Why then am I receiving a cast exception when I call the above code? Am I overlooking something obvious?

Thanks,
Leo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 5:58 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
You can't do that because an Object array (Object[]) containing values of type Beneficiary is not the same thing as a Beneficiary array (Beneficiary[]). You'll have to cast the individual items as you access them or use the Java5 typesafe collections, i.e., Set<Beneficiary>.

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 8:42 am 
Newbie

Joined: Mon Apr 09, 2007 12:25 pm
Posts: 19
Location: Boston, MA
Gotcha. Thanks!


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.