-->
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.  [ 10 posts ] 
Author Message
 Post subject: Need Help In ClassCastException
PostPosted: Tue May 13, 2008 2:44 am 
Newbie

Joined: Sat Apr 26, 2008 12:16 pm
Posts: 5
while i was working with the oneToMany Releationship , i was getting
an exception as

java.lang.ClassCastException: org.hibernate.collection.PersistentBag
cannot be cast to java.util.ArrayList

this arrives when i try to get the list of objects .

Can u Plz Provide Me the Solution


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 3:35 am 
Newbie

Joined: Fri Mar 30, 2007 5:57 am
Posts: 5
Hi,

What's the code in your persistent class?

Is it something like:

Code:
public class Example {

    private ArrayList anExampleList;

    public void setAnExampleList(ArrayList anExampleList) {
        this.anExampleList = anExampleList;
    }

    public ArrayList getAnExampleList() {
        return this.anExampleList;
    }
}


Neil


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 4:08 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Looks like you are using a Bag in your mapping file for one-to-many relationship whereas you are trying to assign it to an ArrayList.

Make sure your java bean is in sync with the mapping file.

_________________
Sukirtha


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Wed Aug 26, 2009 5:56 pm 
Newbie

Joined: Sun Aug 23, 2009 8:09 am
Posts: 9
"org.hibernate.collection.PersistentBag cannot be cast to java.util.ArrayList"


I have exactly this same message using annotations when I try to access a List<Object> I have on one of my classes. I had no problems using the List Interface collections until now.

The code is this:

Code:

e.estEmps = (List<EstEmp>)((ArrayList<EstEmp>)this.estEmps).clone();



The collection 'this.estEmps' is retrieved from the database, it usually works, so it's correctly mapped, but this time I needed to make a clone() of the complete List collection, so I made the cast to ArrayList and found that it throwed an exception.

I need to return a List for the assignment on the left side, so I don't mind changing the Collection, but I never used the PersistentBag and don't know how to handle in order to get a Collection implementing the List interface. I looked the reference and didn't find anything returning List.

Please help!


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Thu Aug 27, 2009 6:39 am 
Newbie

Joined: Sun Aug 23, 2009 8:09 am
Posts: 9
I have done my homework and read the Manning and I found that if I want to use a List and not to be used as a PersistentBag by Hibernate I have to initialize as

List<MyObject> = new ArrayList<MyObject>();

and use the annotation

@org.hibernate.annotations.IndexColumn(name="INDEXCOLUMN")

My problem is that I don't have this column not anything similar on my database (unless I use my composite Id to make an index, which would be too strange...). Should I change to another Collection like Set? I wouldn't mind because I don't have any important order or sort method, but I don't know if clone() will work the same or if there are other disadvantages.

Could anyone give me some advice, please?

EDIT: I've tried the same using the Set interface and the HashSet Implementation. No changes. This is the exception.

org.hibernate.collection.PersistentSet cannot be cast to java.util.HashSet


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Thu Aug 27, 2009 9:09 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Is this in a distributed environment by any chance? Is it the web end that is getting this problem?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Thu Aug 27, 2009 10:04 am 
Newbie

Joined: Sun Aug 23, 2009 8:09 am
Posts: 9
This is happening in a web application which I'm developing in my laptop. I'm a student at the faculty. The fact is that everything is running on a single computer which is this laptop.

The error appears through the web as a Tapestry exception pointing to the Hibernate cause of not being able to cast th persistent bag to an ArrayList when I try to clone() the estEmps List, which should be an ArrayList, as I initialized in the source code.

I don't know if that answers to your question. Maybe my understanding of English is not enough.


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Mon Sep 14, 2009 3:39 pm 
Newbie

Joined: Sun Apr 06, 2008 8:49 am
Posts: 3
The following should get you around the org.hibernate.collection.PersistentSet cannot be cast to java.util.HashSet error (obviously you'll need to change object/method names to match your project):

Set<YourObject> aSet = new HashSet<YourObject>();
aSet.addAll((Set) modelObject.getSetOfObjects());


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Fri Oct 16, 2009 8:32 am 
Newbie

Joined: Fri Sep 18, 2009 11:21 pm
Posts: 2
EDIT: I was wrong.


Top
 Profile  
 
 Post subject: Re: Need Help In ClassCastException
PostPosted: Mon Mar 19, 2012 11:02 am 
Newbie

Joined: Fri Oct 14, 2011 8:07 am
Posts: 1
if you need, you can iterate your list like as:
Code:
for(Object o : yourListObject){
  o.myProperty();
}

without convert or cast to anything...


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