-->
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.  [ 4 posts ] 
Author Message
 Post subject: Using toArray(Object[]) with a property that is a List
PostPosted: Tue May 10, 2005 1:28 pm 
Newbie

Joined: Tue May 10, 2005 1:16 pm
Posts: 16
Location: Vancouver, BC, Canada
I have a hierarchy that is a Composition pattern that uses the standard way in the hibernate documentation for the Bag of children.

As the Composition pattern goes, eveything is a subclass of an abstract Person class.

Now, I want to getChildren().toArray(new Women[0]); to get all the children that are instances of the subclass Women of Person.

Only I get an ArrayStoreException when I execute the above line.

I have moved to Hibernate from a roll-my-own persistence layer, and don't understand why I can't do this. This is used in many key places in my application, maybe there is a better Java way that I should be doing things.

If it is something to do with casting to a subclass then I have been unable find a clear outline in the hibernate documentation as to what you can't do.

I willl try to put together a full example, but I would expect someone that is an expert to get what I'm talking about.

Many thanks,

jAs

_________________
http://www.JamesAshepherd.com/


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 1:41 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
The List.toArray( ) method does not work like that. It will not pick and choose and only return members that are of the specified type. It tries to cast all of its members to the specified type and throws an ArrayStoreException if it is unable to do so.

"[Throws] ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list."

[url]http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html#toArray(java.lang.Object[])[/url]

(Emphasis mine)

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 1:53 pm 
Newbie

Joined: Tue May 10, 2005 1:16 pm
Posts: 16
Location: Vancouver, BC, Canada
Ever have that feeliing that the floor has just disappeared - I'm was certain I've used toArray tens of times like that, oh welll, must be hallucinating. Re-reading the spec you are of course correct. Now I have to find out why I get the error in only one of the three places I have changed to using toArray(), must be coincidence that it works! How embarassing.

_________________
http://www.JamesAshepherd.com/


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 2:16 pm 
Newbie

Joined: Tue May 10, 2005 1:16 pm
Posts: 16
Location: Vancouver, BC, Canada
And now I get ClassCastExceptions, but the answer well documented and is to be found in using Visitor which is overkill for what I want, but looks like best way.

Thanks

_________________
http://www.JamesAshepherd.com/


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